Code Example |
1 // Bridge 2 3 // Intent: "Decouple an abstraction from its implemntation so that the 4 // two can vary independently". 5 6 // For further information, read "Design Patterns", p151, Gamma et al., 7 // Addison-Wesley, ISBN:0-201-63361-2 8 9 /**//* Notes: 10 * Coupling between classes and class libraries is a major maintenance 11 * headache. To ease this problem, often the client talks to an 12 * abstraction description, which in turn calls an implementation. 13 * Sometimes these must evolve - when one changes there can be a need 14 * to change the other. The bridge design pattern lets the abstraction 15 * and its implementation evolve separately. 16 * 17 * So, what is the difference between a bridge and an interface? Interfaces 18 * can be used when creating bridges - but it should be noted that bridges 19 * have additional possibilities. Both the abstraction and the 20 * implementation may evolve over time and be the parent of derived classes. 21 * The operations needed in the implementation could be defined in an 22 * interface if there are no standard methods which are available at the 23 * top-level of the implementation. 24 * 25 */ 26 27 namespace Bridge_DesignPattern 28  { 29 using System; 30 31 class Abstraction 32 { 33 protected Implementation impToUse; 34 35 public void SetImplementation(Implementation i) 36 { 37 impToUse = i; 38 } 39 40 virtual public void DumpString(string str) 41 { 42 impToUse.DoStringOp(str); 43 } 44 } 45 46 class DerivedAbstraction_One : Abstraction 47 { 48 override public void DumpString(string str) 49 { 50 str += ".com"; 51 impToUse.DoStringOp(str); 52 } 53 } 54 55 class Implementation 56 { 57 public virtual void DoStringOp(string str) 58 { 59 Console.WriteLine("Standard implementation - print string as is"); 60 Console.WriteLine("string = {0}", str); 61 } 62 } 63 64 class DerivedImplementation_One : Implementation 65 { 66 override public void DoStringOp(string str) 67 { 68 Console.WriteLine("DerivedImplementation_One - don't print string"); 69 } 70 } 71 72 class DerivedImplementation_Two : Implementation 73 { 74 override public void DoStringOp(string str) 75 { 76 Console.WriteLine("DerivedImplementation_Two - print string twice"); 77 Console.WriteLine("string = {0}", str); 78 Console.WriteLine("string = {0}", str); 79 } 80 } 81 82 /**//// <summary> 83 /// Summary description for Client. 84 /// </summary> 85 public class Client 86 { 87 Abstraction SetupMyParticularAbstraction() 88 { 89 // we localize to this method the decision which abstraction and 90 // which implementation to use. These need to be decided 91 // somewhere and we do it here. All teh rest of the client 92 // code can work against the abstraction object. 93 Abstraction a = new DerivedAbstraction_One(); 94 a.SetImplementation(new DerivedImplementation_Two()); 95 return a; 96 } 97 98 public static int Main(string[] args) 99 { 100 Client c = new Client(); 101 Abstraction a = c.SetupMyParticularAbstraction(); 102 103 // From here on client code thinks it is talking to the 104 // abstraction, and will not need to be changed as 105 // derived abstractions are changed. 106 107 // more client code using the abstraction goes here 108 // . . . 109 a.DumpString("Clipcode"); 110 111 return 0; 112 } 113 } 114 } 115 116 |