Structure |
|
Intention |
Separate the abstract part from its implementation, so that they can all change independently. |
Applicability |
- You don't want to have a fixed binding relationship between the abstraction and its implementation section. For example, this may be because the implementation part of the program run time should be selectable or switched.
- The abstraction of a class and its implementation should be augmented by methods that generate subclasses. At this point, the B r i d g E mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
- Modifications to an abstract implementation section should not have an impact on the customer, i.e. the customer's code does not have to be recompiled.
- (C + +) you want to completely hide the abstract implementation part from the customer. In C + +, the representation of a class is visible in the class interface.
- There are many classes to build. Such a class hierarchy illustrates that you must break up an object into two parts. R u m b a u g H call this class hierarchy "nested nested" (generalizations).
- You want to share the implementation across multiple objects (you might use reference counting), but you're asking customers not to know that. A simple example is C o p L i e n S t r i n g class [C O P 9 2], in which multiple objects can share the same string representation (s t r i n g r e P).
|
1 usingSystem;2 3 classAbstraction4 {5 protectedimplementation Imptouse;6 7 Public voidsetimplementation (Implementation i)8 {9Imptouse =i;Ten } One A Virtual Public voidDumpstring (stringstr) - { - Imptouse.dostringop (str); the } - } - - classderivedabstraction_one:abstraction + { - Override Public voidDumpstring (stringstr) + { Astr + =". com"; at Imptouse.dostringop (str); - } - } - - classImplementation - { in Public Virtual voidDOSTRINGOP (stringstr) - { toConsole.WriteLine ("Standard Implementation-print string as is"); +Console.WriteLine ("string = {0}", str); - } the } * $ classderivedimplementation_one:implementationPanax Notoginseng { - Override Public voidDOSTRINGOP (stringstr) the { +Console.WriteLine ("Derivedimplementation_one-don ' t print string"); A } the } + - classderivedimplementation_two:implementation $ { $ Override Public voidDOSTRINGOP (stringstr) - { -Console.WriteLine ("Derivedimplementation_two-print string twice"); theConsole.WriteLine ("string = {0}", str); -Console.WriteLine ("string = {0}", str);Wuyi } the } - Wu /// <summary> - ///Summary description for Client. About /// </summary> $ Public classClient - { - Abstraction setupmyparticularabstraction () - { A //We localize to this method the decision which abstraction and + //which implementation to use. These need to be decided the //somewhere and we do it here. All teh rest of the client - //code can work against the abstraction object. $Abstraction A =NewDerivedabstraction_one (); theA.setimplementation (Newderivedimplementation_two ()); the returnA; the } the - Public Static intMain (string[] args) in { theClient C =NewClient (); theAbstraction A =c.setupmyparticularabstraction (); About the //From this on client code thinks it's talking to the the //abstraction, and won't need to be changed as the //derived abstractions is changed. + - //More client code using the abstraction goes here the // . . . BayiA.dumpstring ("Clipcode"); the the return 0; - } -}
Bridging Mode
Bridge mode of structural design mode