当前位置:网站首页>桥接模式(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;
}
三、优缺点,适用场景
优点
- 符合开闭原则,抽象与实现分离,扩展能力强。
- 单一职责原则,抽象部分专注于处理高层逻辑,实现部分处理平台细节。
缺点
- 由于聚合关系建立在抽象层,要求开发者能正确地识别出系统中两个独立变化的维度。
- 对高内聚的类使用该模式可能会让代码更加复杂。
边栏推荐
- 深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别
- What are the common categories of software testing?
- H - Sumsets POJ 2229
- A must for programmers, an artifact utools that can improve your work efficiency n times
- Power Designer - Custom Comment button
- MySQL 自定义函数时:This function has none of DETERMINISTIC, NO SQL 解决方案
- C structure: definition and example
- 【网络是怎么连接的】第一章:浏览器生成消息
- Stream learning record
- goto语句实现关机小程序
猜你喜欢

el-form-item 包含两个input, 校验这两个input

P5733 【深基6.例1】自动修正

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

Stream流学习记录

goto语句实现关机小程序

IDC报告:百度智能云AI Cloud市场份额连续六次第一

Don't mess with full_ Case and parallel_ CASE

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

Explain C language 10 in detail (C language series)

机器学习笔记 - 时间序列的季节性
随机推荐
Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
KVM 显卡透传 —— 筑梦之路
Machine learning notes - seasonality of time series
J - Wooden Sticks poj 1065
中国剩余定理模板题 互质与非互质
Learning Processing Zoog
倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
Don't mess with full_ Case and parallel_ CASE
Sharing ideas for a quick switch to an underlying implementation
Vivado 错误代码 [DRC PDCN-2721] 解决
Solution of Splunk iowait alarm
机器学习笔记 - 时间序列的季节性
Explain C language 11 in detail (C language series)
Processing 多面体变化
IDC报告:百度智能云AI Cloud市场份额连续六次第一
Chapter 01_ Installation and use of MySQL under Linux
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
自动化测试的局限性你知道吗?
单例的常用创建和使用方式
Electron official docs series: Examples