当前位置:网站首页>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 .
边栏推荐
- Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
- Processsing 鼠标交互 学习
- Go structure method
- HDU 5860
- Adobe Acrobat prevents 30 security software from viewing PDF files or there are security risks
- 国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
- 解中小企业之困,百度智能云打个样
- 体现技术深度(无法速成)
- mariadb学习笔记
- Guacamole installation
猜你喜欢

scrapy——爬取漫画自定义存储路径下载到本地

桥接模式(Bridge)

Electron official docs series: Processes in Electron
![Vivado 错误代码 [DRC PDCN-2721] 解决](/img/de/ce1a72f072254ae227fdcb307641a2.png)
Vivado 错误代码 [DRC PDCN-2721] 解决

File remote synchronization and backup artifact Rsync

. Net Maui performance improvement

倍福CX5130换卡对已有的授权文件转移操作

5月产品升级观察站

Don't mess with full_ Case and parallel_ CASE

OPLG: 新一代云原生可观测最佳实践
随机推荐
C structure: definition and example
深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别
Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
Deep parsing MySQL binlog
. Net Maui performance improvement
Electron official docs series: Contributing
倍福Ethercat模块网络诊断和硬件排查的基本方法
Go structure method
tauri vs electron
解中小企业之困,百度智能云打个样
第01章_Linux下MySQL的安装与使用
HDU 3555 Bomb
C - Common Subsequence
Electron official docs series: Get Started
PostGIS calculation angle
【网络是怎么连接的】第二章(下):一个网络包的接收
倍福NC轴状态转移图解析
UVA5009 Error Curves三分
The El form item contains two inputs. Verify the two inputs
自定义封装下拉组件