当前位置: 首页 > 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.lryc.cn/news/592592.html

相关文章:

  • 关于在VScode中使用git的一些步骤常用命令及其常见问题:
  • Redis工具类
  • RHCE第二次作业
  • MyBatis:配置文件完成增删改查_添加
  • Java 核心工具类 API 详解(一):从 Math 到 Runtime 的实用指南
  • 谷歌浏览器Chrome的多用户配置文件功能
  • 简单易懂,基本地址变换机构
  • 高防IP能够防御CC攻击吗?它具备哪些显著优势?
  • 【easytokenizer】高性能文本 Tokenizer库 | 源码详解与编译安装
  • Java中类加载器及双亲委派机制原理
  • 2023 年 3 月青少年软编等考 C 语言八级真题解析
  • Windows8.1安装哪个版本的vscode?
  • 基于华为openEuler系统安装DailyNotes个人笔记管理工具
  • HTML常见标签
  • 关于Mysql开启慢查询日志报错:13 - Permission denied的解决方案
  • 爬虫小知识(二)网页进行交互
  • 前端流式渲染流式SSR详解
  • 模板初阶和C++内存管理
  • 【软件重构】如何避免意外冗余
  • 高速公路自动化安全监测主要内容
  • A33-vstar报错记录:ERROR: build kernel Failed
  • 深入理解Linux文件I/O:系统调用与标志位应用
  • 广东省省考备考(第四十九天7.18)——判断推理:位置规律(听课后强化训练)
  • *SFT深度实践指南:从数据构建到模型部署的全流程解析
  • Linux | Bash 子字符串提取
  • Redis原理之哨兵机制(Sentinel)
  • Android性能优化之网络优化
  • 【锂电池剩余寿命预测】TCN时间卷积神经网络锂电池剩余寿命预测(Pytorch完整源码和数据)
  • 如何用Python并发下载?深入解析concurrent.futures 与期物机制
  • 安卓Android项目 报错:系统找不到指定文件