当前位置:网站首页>桥接模式(Bridge)
桥接模式(Bridge)
2022-06-26 12:40: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;
}
三、优缺点,适用场景
优点
- 符合开闭原则,抽象与实现分离,扩展能力强。
- 单一职责原则,抽象部分专注于处理高层逻辑,实现部分处理平台细节。
缺点
- 由于聚合关系建立在抽象层,要求开发者能正确地识别出系统中两个独立变化的维度。
- 对高内聚的类使用该模式可能会让代码更加复杂。
边栏推荐
- QT .pri 的建立与使用
- Processing 多面体变化
- ES6:迭代器
- 倍福TwinCAT3实现CSV、TXT文件读写操作
- Machine learning notes - seasonality of time series
- 【网络是怎么连接的】第二章(下):一个网络包的接收
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- Stream learning record
- Detailed explanation of C const: definition and use of C constant
- [BSidesCF 2019]Kookie 1
猜你喜欢

【网络是怎么连接的】第一章:浏览器生成消息

【Spark】.scala文件在IDEA中几种图标的解释

. Net Maui performance improvement

ES6:Map

National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair

A must for programmers, an artifact utools that can improve your work efficiency n times

C# const详解:C#常量的定义和使用

This function has none of deterministic, no SQL solution

【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
What should the software test report include? Interview must ask
随机推荐
P5733 【深基6.例1】自动修正
软件测试报告应该包含的内容?面试必问
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
微信小程序测试点总结
Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control
processing 随机生成线动画
P2393 yyy loves Maths II
Summary of some application research cases of UAV Remote Sensing in forest monitoring
Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证
KVM 显卡透传 —— 筑梦之路
倍福PLC基于NT_Shutdown实现控制器自动关机重启
Stream learning record
四类线性相位 FIR滤波器设计 —— MATLAB源码全集
使用SSH密钥对登陆服务器
goto语句实现关机小程序
倍福PLC基于CX5130实现数据的断电保持
C - Common Subsequence
openlayers 绘制动态迁徙线、曲线