当前位置:网站首页>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
// Implementation class interfaceclass Game{public:Game(){}virtual void play() = 0;private:};// Concrete implementation class GameAclass GameA:public Game{public:GameA(){}void play(){printf("Jungle Play a game A\n");}};// Concrete implementation class GameBclass GameB :public Game{public:GameB(){}void play(){printf("Jungle Play a game B\n");}};
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
// abstract class Phoneclass Phone{public:Phone(){}// install gamesvirtual void setupGame(Game *igame) = 0;virtual void play() = 0;private:Game *game;};// Extended abstract class PhoneAclass PhoneA:public Phone{public:PhoneA(){}// install gamesvoid setupGame(Game *igame){this->game = igame;}void play(){this->game->play();}private:Game *game;};// Extended abstract class PhoneBclass PhoneB :public Phone{public:PhoneB(){}// install gamesvoid setupGame(Game *igame){this->game = igame;}void play(){this->game->play();}private:Game *game;};
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
#include <iostream>#include "BridgePattern.h"int main(){Game *game;Phone *phone;//Jungle bought PhoneA Brand phones , Want to play games Aphone = new PhoneA();game = new GameA();phone->setupGame(game);phone->play();printf("++++++++++++++++++++++++++++++++++\n");//Jungle Want to play games on this mobile phone Bgame = new GameB();phone->setupGame(game);phone->play();system("pause");return 0;}
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 .
边栏推荐
- 16 解释器模式
- QT custom composite control (class promotion function)
- Daily learning-01
- Fusing and current limiting
- Spark Yarn内存资源计算分析(参考)--Executor Cores、Nums、Memory优化配置
- Detailed explanation of the underlying principle of concurrent thread pool and source code analysis
- Golang 开发 常用的第三方库 没有最全只有更全
- swagger中的枚举、自定义类型和swaggerignore
- steam教育文化传承的必要性
- How to select the appropriate partition key, routing rule and partition number
猜你喜欢

Implementation and landing of any to any real-time voice change RTC dev Meetup

Spark Yarn内存资源计算分析(参考)--Executor Cores、Nums、Memory优化配置

Mysql5.6.36 tutorial

Flask博客实战 - 实现博客的分类管理

What actions might cause thread context switching?

QT custom composite control (class promotion function)

邮件巨头曝严重漏洞,用户数据被窃取

并发三大特性1-可见性

Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security

11 外观模式
随机推荐
Bit group sort
Flask博客实战 - 实现文章管理
The necessity of steam education culture inheritance
377.组合总和 Ⅳ
培养以科学技能为本的Steam教育
Do not use primitive types in new code during the use of generic types
Basic knowledge of DDL operation database and operation table and explanation of use format
Nisp online simulation question bank
解析认知理论对创客教师实训的作用
Remove the restriction of video memory occupied by tensorflow GPU
Develop steam education based on scientific skills
MySQL sub database and sub table
Web knowledge 4 (filter+listener)
Introduction to MySQL database Basics
解读创客教育中的技术一族
C # realizes voice reading function
Analyzing the role of cognitive theory in maker teacher training
19 备忘录模式
Example of QT qtableview
矩阵分解