Explanation of the Bridge Mode in the C ++ Design Mode

Source: Internet
Author: User

Explanation of the Bridge Mode in the C ++ Design Mode
C ++ design mode: Bridging Mode

Separate the abstract part from its implementation part so that they can all change independently.

 

I. Reasons

Assume that you need a large, medium, small, or three paint brushes to draw 7 different colors. If you want to use crayons, you need to prepare 3x7 = 21 crayons. If you use a brush, you only need three brushes and seven medium pigments. Obviously, using a brush is much easier than using a crayons, becauseIn the crayon object, the model and paint are too tightly coupled by two different dimensions, while the model and color of the brush are loosely coupled.In the DRDs instance, the model size and color are separated and can be changed independently. This is the basic idea of the Bridge Mode:The roles and responsibilities of different dimensions are extracted to form their respective inheritance level structures, so that they can be changed independently.

II. Implementation

The following figure shows the UML class diagram of the Bridge Mode:

We can see that in the bridge mode, there are two inheritance trees left and right,AbstractionAndImplementorThe two inheritance trees can develop and change independently without interfering with each other.
In such a scenario, the audio decoding algorithms are consistent on different operating systems, but the playing methods are inconsistent. To enable a player to play music in different formats on different operating systems, there are two changing dimensions: audio format and operating system. Before learning the bridge mode, our solution is as follows:

Obviously, because weAudio DecodingAndPlay audioThe responsibility is designed to be strongly coupled, so our system structure becomes complicated, which is a violationSingle responsibility. Therefore, we used the bridging mode to extract the responsibility of playing audio in the operating system and become a new inheritance tree. This allows for independent changes in the two roles of playing audio and decoding. In this way, the audio playing part of the player is removed, that isAbstraction and implementationSeparated. The following figure shows the UML class diagram using the Bridge Mode:

We can see that the solution after the bridge mode is used is very clear. The benefits of the bridge mode are evident.

Iii. Code

The following is a sample code of the bridge mode.

# Include
  
   
# Include
   
    
Using std: string; class OperatinSystem {public: OperatinSystem () {}; virtual ~ OperatinSystem () {}; virtual void doPlay () = 0 ;}; class Linux: public OperatinSystem {public: Linux () {}; virtual ~ Linux () {}; virtual void doPlay () {std: cout <"Linux" <std: endl ;}}; class Unix: public OperatinSystem {public: unix () {}; virtual ~ Unix () {}; virtual void doPlay () {std: cout <"Unix" <std: endl ;}; class Windows: public OperatinSystem {public: windows () {}; virtual ~ Windows () {}; virtual void doPlay () {std: cout <"Windows" <std: endl ;}}; class Player {protected: OperatinSystem * OS; public: Player (): OS (NULL) {}; virtual void setOS (OperatinSystem * OS) {OS = OS;} virtual ~ Player () {}; virtual void play () = 0 ;}; class MP3Player: public Player {public: MP3Player () {}; virtual ~ MP3Player () {}; virtual void play () {std: cout <"Play MP3 under"; OS-> doPlay () ;}}; class WAVPlayer: public Player {public: WAVPlayer () {}; virtual ~ WAVPlayer () {}; virtual void play () {std: cout <"Play WAV under"; OS-> doPlay () ;}}; class WMAPlayer: public Player {public: WMAPlayer () {}; virtual ~ WMAPlayer () {}; virtual void play () {std: cout <"Play WMA under"; OS-> doPlay () ;}}; int main (void) {Player * mp3Player = new MP3Player; mp3Player-> setOS (new Linux); mp3Player-> play (); mp3Player-> setOS (new Windows); mp3Player-> play (); mp3Player-> setOS (new Unix); mp3Player-> play (); Player * wmalayer = new WMAPlayer; wmalayer-> setOS (new Linux); wmalayer-> play (); wmalayer-> setOS (new Windows); wmalayer-> play (); wmalayer-> setOS (new Unix); wmalayer-> play (); Player * wavPlayer = new WAVPlayer; wavPlayer-> setOS (new Linux); wavPlayer-> play (); wavPlayer-> setOS (new Windows); wavPlayer-> play (); wavPlayer-> setOS (new Unix); wavPlayer-> play ();} running result: play MP3 under LinuxPlay MP3 under WindowsPlay MP3 under UnixPlay WMA under LinuxPlay WMA under WindowsPlay WMA under UnixPlay WAV under LinuxPlay WAV under WindowsPlay WAV under Unix
   
  
Iv. Summary

Advantages of the Bridge Mode:

Abstract and implementation are separated. The bridge mode uses aggregation instead of the binding relationship between the original classes (inheritance is invasive) to achieve loose coupling.

The changing factors are isolated from each other, independent, and easy to expand. When a new implementation of the system is added in the bridge mode, you do not need to modify the existing class, which complies with the open and closed principles.

Applicable scenarios of the Bridge Mode:

When the inheritance tree is too complex, you should consider using the bridge mode. When a class has multiple changing dimensions, the bridge mode should be considered.

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.