"Structural" bridge mode

Source: Internet
Author: User

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 Bridge
Bridge Pattern Coding structure Reference

"Structural" bridge mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.