设计模式五:桥模式(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开发、驱动程序开发等领域有广泛应用,例如不同操作系统下的窗口实现

相关推荐
智驱力人工智能5 小时前
工厂智慧设备检测:多模态算法提升工业安全阈值
人工智能·算法·安全·边缘计算·智慧工厂·智能巡航·工厂设备检测
IT小白架构师之路6 小时前
常用设计模式系列(十八)-责任链模式
设计模式·责任链模式
2501_924731478 小时前
城市路口识别准确率↑31%!陌讯时空建模算法在交通拥堵识别中的突破
人工智能·算法·目标检测·计算机视觉·目标跟踪
熬了夜的程序员8 小时前
【华为机试】208. 实现 Trie (前缀树)
数据结构·算法·华为od·华为
小O的算法实验室10 小时前
2024年ESWA SCI1区TOP,自适应种群分配和变异选择差分进化算法iDE-APAMS,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
不吃洋葱.11 小时前
左子树之和
算法
金融小师妹12 小时前
基于AI量化模型的比特币周期重构:传统四年规律是否被算法因子打破?
大数据·人工智能·算法
数据智能老司机13 小时前
图算法趣味学——最短路径
数据结构·算法·云计算
快去睡觉~13 小时前
力扣109:有序链表转换二叉搜索树
算法·leetcode·链表
是Dream呀13 小时前
YOLOv8深度解析:从架构革新到应用实践
人工智能·算法