当前位置:网站首页>08 bridging mode

08 bridging mode

2022-06-22 08:29:00 Grow some hair.

Reprint of the original :https://blog.csdn.net/sinat_21107433/article/details/102694306

Jungle There are two mobile phones , Namely M Mobile phones and N mobile phone ,M There are games on the mobile phone Game1,N It's on the phone Game2. Every time Jungle Want to play Game1 when , Just use M mobile phone , Want to play Game2 when , Just play N mobile phone . If one day Jungle Go out , In a good mood , I want to play both games , that Jungle I have to bring two mobile phones ??? Ma does not bother ?

If a new game comes out Game3, that Jungle Do you want to buy another mobile phone ?

They are all game software , Why not put all the games on one mobile phone ?

1. Introduction to bridge mode

If a class in the system has two independently changing dimensions , These two dimensions can be separated through the bridge pattern , Make the two extend independently . As in the above example ,Jungle Want to play games on your mobile phone , Mobile phones and games are two independent dimensions , Adding a game has no effect on the mobile phone , Adding a mobile phone has no effect on the game . Games can be installed on mobile phones , Games must be played on mobile phones , From this point of view , There is a strong coupling between mobile phones and games .

But the two can be well decoupled , And it can be expanded flexibly after decoupling : All games are installed on one mobile phone , A new game , New installation ok! Bought a new cell phone , You can also install all the games . This is the bridging mode :

Bridging mode :

Decouple the abstract from its implementation , So that both can change independently .

Bridge The connection pattern designs two independent variable dimensions into two independent inheritance hierarchies ( Instead of coupling the two together to form a multi-layer inheritance structure ), In the abstraction layer, an abstract association is established between the two , The relationship is similar to a bridge , Connect two independent hierarchical structures , So it is said that “ Bridging mode ”.

2. Bridge mode structure

Bridging mode UML The figure is shown in the figure below . It can be seen from the picture that , Bridging mode includes the following roles :

  • Abstraction( abstract class ): Define the interface of an abstract class ( Abstract interface ), From the aggregation relationship , The abstract class contains a Implementor Object of type , It is associated with Implementor There is a correlation between , It can include abstract business methods , Specific business methods can also be included ;
  • Implementor( Implementation class interface ): Define the interface that implements the class , This interface can be connected with Abstraction Class has different interfaces . generally speaking , The implementation class interface defines only basic operations , The interface of the abstract class may also do more complex operations .
  • RefinedAbstraction( Extended abstract class ): concrete class , Implement the interface defined in the abstract class , You can call in Implementor The method defined in ;
  • ConcreteImplementor( Concrete implementation class ): The concrete realization of Implementor Interface , Implement different concrete operations in different concrete implementation classes . Runtime ConcreteImplementor The parent class will be replaced .

in short , stay Abstraction Maintain one in the class Implementor Class pointer , When you need to adopt different implementation methods, you only need to pass in different Implementor Just derived classes .

3. Bridge mode code example

Take the story in the introduction as an example ,Jungle Learning the bridge mode is very enlightening , You want to do the following :

New phones can be quickly installed on new phones (setup) And play (play) game

When adding a new game Jungle It can be installed on existing mobile phones and play

In this case , The cell phone is abstract class Abstraction, Have the ability to play games Implementation class interface Implementor, Different mobile phone brands Extended abstract class RefinedAbstraction, Many different games are Concrete implementation class ConcreteImplementor.

3.1. Implementation class

 
  1. // Implementation class interface

  2. class Game

  3. {

  4. public:

  5. Game(){}

  6. virtual void play() = 0;

  7. private:

  8. };

  9.  
  10. // Concrete implementation class GameA

  11. class GameA:public Game

  12. {

  13. public:

  14. GameA(){}

  15. void play(){

  16. printf("Jungle Play a game A\n");

  17. }

  18. };

  19.  
  20. // Concrete implementation class GameB

  21. class GameB :public Game

  22. {

  23. public:

  24. GameB(){}

  25. void play(){

  26. printf("Jungle Play a game B\n");

  27. }

  28. };

Implementation class Game The statement is made. play The interface of , But it is a virtual method , Actually, now the concrete implementation class GameA and GameB In the definition of .

3.2. Abstract classes and extended abstract classes

 
  1. // abstract class Phone

  2. class Phone

  3. {

  4. public:

  5. Phone(){

  6. }

  7. // install games

  8. virtual void setupGame(Game *igame) = 0;

  9. virtual void play() = 0;

  10. private:

  11. Game *game;

  12. };

  13.  
  14. // Extended abstract class PhoneA

  15. class PhoneA:public Phone

  16. {

  17. public:

  18. PhoneA(){

  19. }

  20. // install games

  21. void setupGame(Game *igame){

  22. this->game = igame;

  23. }

  24. void play(){

  25. this->game->play();

  26. }

  27. private:

  28. Game *game;

  29. };

  30.  
  31. // Extended abstract class PhoneB

  32. class PhoneB :public Phone

  33. {

  34. public:

  35. PhoneB(){

  36. }

  37. // install games

  38. void setupGame(Game *igame){

  39. this->game = igame;

  40. }

  41. void play(){

  42. this->game->play();

  43. }

  44. private:

  45. Game *game;

  46. };

abstract class Phone Two virtual methods are also declared in , And defines an object that implements the class , Associating abstractions with implementations . The instantiation of the object is performed when the client uses it .

3.3. Client code example

 
  1. #include <iostream>

  2. #include "BridgePattern.h"

  3.  
  4. int main()

  5. {

  6. Game *game;

  7. Phone *phone;

  8.  
  9. //Jungle bought PhoneA Brand phones , Want to play games A

  10. phone = new PhoneA();

  11. game = new GameA();

  12. phone->setupGame(game);

  13. phone->play();

  14. printf("++++++++++++++++++++++++++++++++++\n");

  15.  
  16. //Jungle Want to play games on this mobile phone B

  17. game = new GameB();

  18. phone->setupGame(game);

  19. phone->play();

  20.  
  21. system("pause");

  22. return 0;

  23. }

3.4. effect

4. Bridge mode summary

advantage :

  • Separate the abstract interface from the implementation part , Use the association relationship between objects to decouple abstraction from implementation ;
  • Bridging mode can replace multi-layer inheritance , Multiple inheritance violates the principle of single responsibility , Not conducive to code reuse ;
  • Bridging mode improves the scalability of the system , If a dimension needs to be extended, you only need to add an implementation class interface or a specific implementation class , And it doesn't affect another dimension , Comply with opening and closing principle .

shortcoming :

  • The bridging pattern is difficult to understand , Because the association relationship is based on the abstraction layer , You need to design the abstraction layer from the beginning ;
  • How to accurately identify the two dimensions in the system is the difficulty of applying bridge mode .

Applicable scenario : 

  • If a system needs to increase flexibility between abstraction and materialization , Avoid adding inheritance between two levels , Bridging patterns can be used to establish associations at the abstraction level ;
  • The abstract part and the implementation part can be extended without affecting each other ;
  • A class has multiple independently varying dimensions , Bridging mode can be adopted .
原网站

版权声明
本文为[Grow some hair.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220821493743.html