当前位置:网站首页>橋接模式(Bridge)
橋接模式(Bridge)
2022-06-26 13:06:00 【baboon_chen】
參考:
橋接模式(Bridge模式)詳解 (biancheng.net)
design-patterns-cpp/bridge at master · JakubVojvoda/design-patterns-cpp · GitHub
一、什麼是橋接模式?
定義:將抽象與實現分離,用組合關系代替繼承,從不同的維度進行擴展。
簡單來說,就是A類中包含有B類接口,通過構造函數傳遞B類的實現,這個B類就是橋。
比如模型玩具有很多種,紅色的球體、藍色的球體、紅色的正方體、藍色的正方體等。可以按形狀、按顏色等兩個維度的進行劃分。A類錶示形狀,B類錶示顏色。(維度可以有多個,以組合方式實現)

二、示例
橋接(Bridge)模式包含以下主要角色:
- 抽象化(Abstraction)角色:定義抽象類,並包含一個對實現化對象的引用。
- 擴展抽象化(Refined Abstraction)角色:是抽象化角色的子類,實現父類中的業務方法,並通過組合關系調用實現化角色中的業務方法。
- 實現化(Implementor)角色:定義實現化角色的接口,供擴展抽象化角色調用。
- 具體實現化(Concrete Implementor)角色:給出實現化角色接口的具體實現。

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
* 實現化角色:定義實現化角色的接口,供擴展抽象化角色調用。
*/
class Implementor
{
public:
virtual ~Implementor() {}
virtual void action() = 0;
// ...
};
/*
* Concrete Implementors
* 具體實現化角色:給出實現化角色接口的具體實現。
*/
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
* 抽象化角色:定義抽象類接口。
*/
class Abstraction
{
public:
virtual ~Abstraction() {}
virtual void operation() = 0;
// ...
};
/*
* RefinedAbstraction
* 擴展抽象化角色:實現父類中的業務接口,並通過組合關系調用實現化角色中的業務方法。
*/
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、不同顏色的模型


#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;
};
// 正方體模型
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;
};
// 球體模型
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;
}
三、優缺點,適用場景
優點
- 符合開閉原則,抽象與實現分離,擴展能力强。
- 單一職責原則,抽象部分專注於處理高層邏輯,實現部分處理平臺細節。
缺點
- 由於聚合關系建立在抽象層,要求開發者能正確地識別出系統中兩個獨立變化的維度。
- 對高內聚的類使用該模式可能會讓代碼更加複雜。
边栏推荐
- 单例的常用创建和使用方式
- POJ 3070 Fibonacci
- 无人机遥感在森林监测的部分应用研究案例总结
- Electron official docs series: Distribution
- Summary of wechat applet test points
- C - Common Subsequence
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- ES6模块
- Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
- 黑马笔记---常用API
猜你喜欢

This function has none of deterministic, no SQL solution

processsing 函数random

Detailed explanation of C const: definition and use of C constant

Photoshop 2022 23.4.1增加了哪些功能?有知道的吗
Summary of wechat applet test points

Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

Electron official docs series: Get Started

Learning Processing Zoog

Word文档导出(使用固定模板)
随机推荐
单例的常用创建和使用方式
OPLG: 新一代云原生可观测最佳实践
C语言:练习题二
倍福NC轴状态转移图解析
F - Charm Bracelet
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
详细讲解C语言10(C语言系列)
Processsing mouse interactive learning
processing 随机生成线动画
Typescript
倍福PLC基于NT_Shutdown实现控制器自动关机重启
Explain C language 11 in detail (C language series)
Software testing - Fundamentals
LeetCode_栈_中等_150. 逆波兰表达式求值
【shell】生成指定日期之间的字符串
Don't mess with full_ Case and parallel_ CASE
postgis计算角度
el-form-item 包含两个input, 校验这两个input
Electron official docs series: Examples