当前位置: 首页 > news >正文

设计模式五:桥模式(Bridge Pattern)

桥模式是一种结构型设计模式,它将抽象部分与其实现部分分离,使它们可以独立变化。这种模式通过提供桥梁结构将抽象和实现解耦。

桥模式的结构

桥模式包含以下主要角色:

  1. Abstraction(抽象类):定义抽象接口,维护一个指向Implementor的指针

  2. RefinedAbstraction(扩充抽象类):扩展Abstraction定义的接口

  3. Implementor(实现类接口):定义实现类的接口

  4. ConcreteImplementor(具体实现类):实现Implementor接口

桥模式的优点

  • 分离抽象接口及其实现部分

  • 提高可扩展性,可以独立扩展抽象和实现部分

  • 实现细节对客户端透明

C++ 桥模式实现示例

#include <iostream>
#include <string>// 实现类接口
class Implementor {
public:virtual ~Implementor() {}virtual std::string operationImpl() const = 0;
};// 具体实现类A
class ConcreteImplementorA : public Implementor {
public:std::string operationImpl() const override {return "ConcreteImplementorA: Here's the result on platform A.\n";}
};// 具体实现类B
class ConcreteImplementorB : public Implementor {
public:std::string operationImpl() const override {return "ConcreteImplementorB: Here's the result on platform B.\n";}
};// 抽象类
class Abstraction {
protected:Implementor* implementor_;public:Abstraction(Implementor* implementor) : implementor_(implementor) {}virtual ~Abstraction() {}virtual std::string operation() const {return "Abstraction: Base operation with:\n" + implementor_->operationImpl();}
};// 扩充抽象类
class ExtendedAbstraction : public Abstraction {
public:ExtendedAbstraction(Implementor* implementor) : Abstraction(implementor) {}std::string operation() const override {return "ExtendedAbstraction: Extended operation with:\n" + implementor_->operationImpl();}
};// 客户端代码
void ClientCode(const Abstraction& abstraction) {std::cout << abstraction.operation();
}int main() {Implementor* implementorA = new ConcreteImplementorA;Abstraction* abstraction = new Abstraction(implementorA);ClientCode(*abstraction);std::cout << "\n";Implementor* implementorB = new ConcreteImplementorB;Abstraction* extendedAbstraction = new ExtendedAbstraction(implementorB);ClientCode(*extendedAbstraction);delete implementorA;delete implementorB;delete abstraction;delete extendedAbstraction;return 0;
}

输出示例

Abstraction: Base operation with:
ConcreteImplementorA: Here's the result on platform A.ExtendedAbstraction: Extended operation with:
ConcreteImplementorB: Here's the result on platform B.

UML结构图 

桥模式的应用场景

  1. 当一个类存在两个独立变化的维度,且这两个维度都需要进行扩展时

  2. 当需要避免在抽象和实现之间形成永久绑定时

  3. 当需要在运行时切换不同的实现时

桥模式在GUI开发、驱动程序开发等领域有广泛应用,例如不同操作系统下的窗口实现

http://www.xdnf.cn/news/1144225.html

相关文章:

  • wedo牛-----第47节(免费分享图纸)
  • MBIST - Memory BIST会对memory进行清零吗?
  • 基于单片机的便携太阳能光伏系统研究
  • C语言—如何生成随机数+原理详细分析
  • 20250718-FDU-HDUOJ钉耙编程一
  • 初探:C语言FILE结构之文件描述符与缓冲区的实现原理
  • 更适合后端宝宝的前端三件套之HTML
  • CentOS7 内网服务器yum修改
  • 有好内容,如何做好知识变现?
  • 【Zephyr开发实践系列】08_NVS文件系统调试记录
  • GEV/POT/Markov/点过程/贝叶斯极值全解析;基于R语言的极值统计学
  • 【案例教程】基于现代R语言【Tidyverse、Tidymodel】的机器学习方法与案例分析实践技术应用
  • [AI8051U入门第五步]modbus_RTU主机
  • stack and queue 之牛刀小试
  • Excel批量生成SQL语句 Excel批量生成SQL脚本 Excel拼接sql
  • 大型市政污水处理厂“智变”记:天拓四方IOT平台让磁悬浮鼓风机“活”起来
  • React 实现人员列表多选、全选与取消全选功能
  • MC0457符咒封印
  • Ansible + Shell 服务器巡检脚本
  • mysql not in 查询引发的bug问题记录
  • pycharm结构查看器
  • 2.3 前端-ts的接口以及自定义类型
  • 哪个厂家生产的戒烟药好:从机制到体验的差异化博弈
  • MySQL 插入时间 更新时间
  • 推荐 1 款 4.5k stars 的AI 大模型驱动的开源知识库搭建系统
  • 跨域问题及解决方案
  • AI(day10)模块化编程概念(模块、包、导入)及常见系统模块总结和第三方模块管理
  • Java Web项目Dump文件分析指南
  • LLM(Large Language Model)大规模语言模型浅析
  • 在 Jenkins 中使用 SSH 部署密钥