Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. class Car {
  2. // ...
  3. private:
  4. Engine engine;
  5. Hood hood;
  6. };
  7.  
  8. // The car is *composed* of an engine and a hood. Hence, composition. You are
  9. // also bringing together (i.e. *aggregating*) an engine and hood into a car.
  10.  
  11. interface Car {
  12. public Engine getEngine();
  13. public Hood getHood();
  14. public void drive();
  15. }
  16. // In the above, the fact that a car has these building blocks
  17. // is a part of its interface (the abstraction).
  18.  
  19. class HondaCivic2010 implements Car {
  20. public void drive(){ getEngine().drive(); }
  21. // ...
  22. }
  23. // In the above, composition/delegation is an implementation
  24. // strategy for providing the drive functionality.
  25.  
  26. class GoodCharacter;
  27. class BadCharacter;
  28. class Mage;
  29. class Rogue;
  30. class GoodMage : public GoodCharacter, Mage;
  31. class BadMage : public BadCharacter, Mage;
  32. class GoodRogue : public GoodCharacter, Rogue;
  33. class BadRogue : public BadCharacter, Rogue;
  34.  
  35. class Personality;
  36. class GoodPersonality : public Personality;
  37. class BadPersonality : public Personality;
  38.  
  39. class CharacterClass;
  40. class Mage : public CharacterClass;
  41. class Rogue : public CharacterClass;
  42.  
  43. class Character {
  44. public:
  45. // ...
  46. private:
  47. CharacterClass character_class;
  48. Personality personality;
  49. };
  50. // A character has both a character class and a personality.
  51. // This is a perfect example of the bridge pattern, and we've
  52. // reduced MxN classes into a mere M+N classes, and we've
  53. // arguably made the system even more flexible than before.
  54.  
  55. * you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
  56.  
  57. * both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
  58.  
  59. * changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
  60.  
  61. * (C++) you want to hide the implementation of an abstraction completely from clients. In C++ the representation of a class is visible in the class interface.
  62.  
  63. * you have a proliferation of classes as shown earlier in the first Motivation diagram. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" [RBP+91] to refer to such class hierarchies.
  64.  
  65. * you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class [Cop92], in which multiple objects can share the same string representation (StringRep).
  66.  
  67. interface ISpeak
  68. {
  69. void Speak();
  70. }
  71.  
  72. class DogSpeak : ISpeak
  73. {
  74. public void Speak()
  75. {
  76. Console.WriteLine("Dog Barks");
  77. }
  78. }
  79. class CatSpeak : ISpeak
  80. {
  81. public void Speak()
  82. {
  83. Console.WriteLine("Cat Meows");
  84. }
  85. }
  86.  
  87. abstract class AnimalBridge
  88. {
  89. protected ISpeak Speech;
  90.  
  91. protected AnimalBridge(ISpeak speech)
  92. {
  93. this.Speech = speech;
  94. }
  95.  
  96. public abstract void Speak();
  97. }
  98. class Dog : AnimalBridge
  99. {
  100. public Dog(ISpeak dogSpeak)
  101. : base(dogSpeak)
  102. {
  103.  
  104. }
  105. public override void Speak()
  106. {
  107. Speech.Speak();
  108. }
  109. }
  110.  
  111. class Cat : AnimalBridge
  112. {
  113. public Cat(ISpeak catSpeak)
  114. : base(catSpeak)
  115. {
  116.  
  117. }
  118. public override void Speak()
  119. {
  120. Speech.Speak();
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement