Bridging patterns are designed to separate the abstraction and implementation of objects so that they can vary independently. Simple words, but already stood at a higher level of abstraction to look at, design, solve problems. Usually we are more on the specific issues of analysis, abstraction, and then began to design, which is basically completely enough for most cases, after all, the actual project function module is to find an "optimal solution" to solve the problem, the function design can be. The structure diagram in this case is referenced below:
The disadvantage of this abstract design is: if there is more than one way to solve the problem, it is necessary for all of the above concrete class is to implement the corresponding specific version, so that not only the class inheritance system is very complex, as long as arbitrarily add a way to implement, will add n corresponding new class. In order to solve this kind of problem, we need to separate the concrete implementation of the problem into a single abstraction, and make an associative bridging in the abstraction level of the problem and the implementation abstraction level. Thus, at the level of abstraction, there is no need to be concerned about how the current problem is solved (that is, how it is implemented). The improved class diagram (that is, the class diagram for bridge mode is referenced below):
As can be seen from the diagram, whether it is to add new target later or expand the new implementation, there is no need to adjust the logical structure, nor because of the expansion of the new implementation to cause the entire target series of turbulence. The coding structure of the pattern is referenced below:
1 namespaceBridge2 {3 classIimpl;4 classITarget5 {6 Public:7 Virtual voidDoSomething () =0;8 //some code here .....9 Ten protected: Oneiimpl* Getimpl () {return_impl;} A - Private: -iimpl*_impl; the -};//class ITarget - - classConcretetarget: PublicITarget + { - Public: + Virtual voidDoSomething ()Override { AAuto Pimpl = This-Getimpl (); at if(Nullptr! =Pimpl) { -Pimpl->realdosomething (); - } - //Some other code here ..... - } - in};//class Concretetarget - to classIimpl + { - Public: the Virtual voidRealdosomething () =0; * $};//class IimplPanax Notoginseng - classCONCRETEIMPL1: PublicIimpl the { + Public: A Virtual voidRealdosomething ()Override{/*some code here .....*/ } the +};//class ConcreteImpl1 - $ classCONCRETEIMPL2: PublicIimpl $ { - Public: - Virtual voidRealdosomething ()Override{/*some code here .....*/ } the -};//class CONCRETEIMPL2Wuyi the}//namespace BridgeBridge Pattern Coding structure Reference
"Structural" bridge mode