当前位置:网站首页>Bridge mode
Bridge mode
2022-06-26 13:06:00 【baboon_ chen】
Reference resources :
Design pattern bridging (refactoringguru.cn)
Bridging mode (Bridge Pattern ) Detailed explanation (biancheng.net)
design-patterns-cpp/bridge at master · JakubVojvoda/design-patterns-cpp · GitHub
One 、 What is bridging mode ?
Definition : Separate abstraction from implementation , Replace inheritance with composition , Extend from different dimensions .
Simply speaking , Namely A Class contains B The class interface , Pass through constructor B The realization of the class , This B Class is bridge .
For example, there are many kinds of model toys , Red sphere 、 Blue sphere 、 Red cube 、 Blue cube, etc . By shape 、 It is divided into two dimensions such as color .A Class represents a shape ,B Class represents color .( A dimension can have multiple , Implement in combination )

Two 、 Example
The bridge (Bridge) The pattern includes the following main characters :
- Abstraction (Abstraction) role : Defining abstract classes , And include a reference to the implementation object .
- Extend abstraction (Refined Abstraction) role : It's a subclass of abstract characters , Implement the business methods in the parent class , And through the combination of relationship calls to realize the role of business methods .
- Realize (Implementor) role : Define the interfaces for implementing roles , For extension Abstract role call .
- Concrete realization (Concrete Implementor) role : Give the implementation of the role interface .

1、Bridge.cpp
/*
* C++ Design Patterns: Bridge
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include <iostream>
/*
* Implementor
* Realize the role : Define the interfaces for implementing roles , For extension Abstract role call .
*/
class Implementor
{
public:
virtual ~Implementor() {}
virtual void action() = 0;
// ...
};
/*
* Concrete Implementors
* Concrete realization role : Give the implementation of the role interface .
*/
class ConcreteImplementorA : public Implementor
{
public:
~ConcreteImplementorA() {}
void action()
{
std::cout << "Concrete Implementor A" << std::endl;
}
// ...
};
class ConcreteImplementorB : public Implementor
{
public:
~ConcreteImplementorB() {}
void action()
{
std::cout << "Concrete Implementor B" << std::endl;
}
// ...
};
/*
* Abstraction
* Abstract character : Define abstract class interfaces .
*/
class Abstraction
{
public:
virtual ~Abstraction() {}
virtual void operation() = 0;
// ...
};
/*
* RefinedAbstraction
* Extend Abstract roles : Implement the business interface in the parent class , And through the combination of relationship calls to realize the role of business methods .
*/
class RefinedAbstraction : public Abstraction
{
public:
~RefinedAbstraction() {}
RefinedAbstraction(Implementor *impl) : implementor(impl) {}
void operation()
{
implementor->action();
}
// ...
private:
Implementor *implementor;
};
int main()
{
Implementor *ia = new ConcreteImplementorA;
Implementor *ib = new ConcreteImplementorB;
Abstraction *abstract1 = new RefinedAbstraction(ia);
abstract1->operation();
Abstraction *abstract2 = new RefinedAbstraction(ib);
abstract2->operation();
delete abstract1;
delete abstract2;
delete ia;
delete ib;
return 0;
}
2、 Models of different colors


#include <iostream>
class Color
{
public:
virtual ~Color() {}
virtual void show() = 0;
};
class Red : public Color
{
public:
~Red() {}
void show() override
{
std::cout << "Color is red." << std::endl;
}
};
class Blue : public Color
{
public:
~Blue() {}
void show() override
{
std::cout << "Color is blue." << std::endl;
}
};
class Model
{
public:
virtual ~Model() {}
virtual void show() = 0;
};
// Cube model
class CubeModel : public Model
{
public:
~CubeModel() {}
CubeModel(Color *c) : color(c) {}
void show() override
{
std::cout << "I am Cube." << std::endl;
color->show();
}
private:
Color *color;
};
// Sphere model
class SphereModel : public Model
{
public:
~SphereModel() {}
SphereModel(Color *c) : color(c) {}
void show() override
{
std::cout << "I am Sphere." << std::endl;
color->show();
}
private:
Color *color;
};
int main()
{
Color *red = new Red();
Color *blue = new Blue();
CubeModel *cube = new CubeModel(red);
cube->show();
SphereModel *sphere = new SphereModel(blue);
sphere->show();
delete cube;
delete sphere;
delete red;
delete blue;
return 0;
}
3、 ... and 、 Advantages and disadvantages , Applicable scenario
advantage
- Comply with opening and closing principle , Separation of abstraction and implementation , Strong expansion ability .
- Principle of single responsibility , The abstract part focuses on high-level logic , Implement part of the processing platform details .
shortcoming
- Because aggregation is based on abstraction , Developers are required to correctly identify two independently changing dimensions in the system .
- Using this pattern for highly cohesive classes can make the code more complex .
边栏推荐
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- I - Dollar Dayz
- . Net Maui performance improvement
- Summary of wechat applet test points
- 倍福PLC旋切基本原理和应用例程
- Don't mess with full_ Case and parallel_ CASE
- QT . Establishment and use of pri
- Software testing - concept
- 微信小程序测试点总结
- Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
猜你喜欢

Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
![[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3](/img/4a/503240b332e3279047c438f1d9845e.png)
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3

10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
![P5733 [deep foundation 6. example 1] automatic correction](/img/34/081dbd805593a92a86c3081d6772e3.png)
P5733 [deep foundation 6. example 1] automatic correction

详细讲解C语言11(C语言系列)

倍福PLC通过MC_ReadParameter读取NC轴的配置参数

Openlayers drawing dynamic migration lines and curves

倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换

Solution of Splunk iowait alarm

机器学习笔记 - 时间序列的季节性
随机推荐
Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
Electron official docs series: References
goto语句实现关机小程序
深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别
Openlayers drawing dynamic migration lines and curves
[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统
Electron official docs series: Distribution
Opencv high speed download
倍福PLC旋切基本原理和应用例程
scrapy——爬取漫画自定义存储路径下载到本地
Vivado 错误代码 [DRC PDCN-2721] 解决
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
P2393 yyy loves Maths II
软件测试测试常见分类有哪些?
Explain C language 10 in detail (C language series)
【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
HDU 3709 Balanced Number
倍福Ethercat模块网络诊断和硬件排查的基本方法
Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)
F - Charm Bracelet