目 录CONTENT

文章目录

桥接模式

WenzhouXv
2023-05-28 / 0 评论 / 0 点赞 / 64 阅读 / 0 字
#include <iostream>
using namespace std;

// 1. 创建实现部分的接口
class Implementor {
public:
    virtual void operationImpl() = 0;
};

// 2. 实现部分具体类A
class ConcreteImplementorA : public Implementor {
public:
    void operationImpl() override {
        cout << "Concrete Implementor A" << endl;
    }
};

// 3. 实现部分具体类B
class ConcreteImplementorB : public Implementor {
public:
    void operationImpl() override {
        cout << "Concrete Implementor B" << endl;
    }
};

// 4. 抽象部分的接口
class Abstraction {
protected:
    Implementor* implementor;
public:
    void setImplementor(Implementor* impl) {
        implementor = impl;
    }
    virtual void operation() = 0;
};

// 5. 抽象部分具体类
class AbstractionImpl : public Abstraction {
public:
    void operation() override {
        implementor->operationImpl();
    }
};

// 6. 客户端代码
int main() {
    Abstraction* abs = new AbstractionImpl();
    abs->setImplementor(new ConcreteImplementorA());
    abs->operation();

    abs->setImplementor(new ConcreteImplementorB());
    abs->operation();

    // 释放内存
    delete abs->implementor;
    delete abs;
    return 0;
}
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区