桥接模式(C++)

#include <iostream>
using namespace std;

class software
{
public:
    software(){}
    virtual ~software(){}
    virtual void exec()=0;
};

class game : public software
{
public:
    game(){}
    virtual ~game(){}
    void exec(){cout<<"play game"<<endl;}
};

class office : public software
{
public:
    office(){}
    virtual ~office(){}
    void exec(){cout<<"work office"<<endl;}
};

class computer
{
public:
    computer():_instance(NULL){}
    virtual ~computer(){}
    void setsoftware(software *instance){_instance=instance;}
    virtual void Exec()=0;

protected:
    software *_instance;
};

class wincomputer : public computer
{
public:
    wincomputer(){}
    virtual ~wincomputer(){}
    void Exec(){cout<<"win  ";_instance->exec();}
};

class maccomputer : public computer
{
public:
    maccomputer(){}
    virtual ~maccomputer(){}
    void Exec(){cout<<"mac  ";_instance->exec();}
};

int main()
{
    computer *pc=new wincomputer;
    software *ps=new office;
    pc->setsoftware(ps);
    pc->Exec();
    delete ps;
    delete pc;

    pc=new maccomputer;
    ps=new game;
    pc->setsoftware(ps);
    pc->Exec();
    delete ps;
    delete pc;

    system("pause");
    return 0;
}
### C++桥接模式的核心概念 桥接模式是一种结构型设计模式,其主要目的是将抽象部分与其实现部分分离,使两者可以独立变化。这种模式通过引入两个层次的类体系——抽象层和实现层——来达到解耦的目的[^1]。 #### 核心思想 桥接模式的关键在于将系统的多个维度(如图形和绘制方式)分离开来,使得它们能够各自独立发展而不互相影响。这样做的好处是可以减少代码复杂度并提高可扩展性[^4]。 --- ### 桥接模式的角色划分 在 C++桥接模式中,通常会涉及到以下四个角色: 1. **抽象类接口 (Abstraction)** 定义抽象部分的接口,并维护一个指向实现部分的指针。它负责声明高层的操作方法,而这些操作的具体实现则由具体子类完成[^3]。 2. **具体抽象类 (ConcreteAbstraction)** 继承自抽象类,用于细化或增强抽象类的功能。它可以调用实现类中的方法以完成某些功能。 3. **实现类接口 (Implementor)** 提供实现部分的接口定义。所有的具体实现都需遵循此接口的规定。 4. **具体实现类 (ConcreteImplementor)** 实现 `Implementor` 接口,提供具体的业务逻辑处理。 --- ### 应用场景分析 桥接模式适用于以下情况: - 当系统需要支持多种不同类型的对象组合时,比如不同的形状和颜色[^2]。 - 需要动态切换实现细节,而不需要改变客户端代码的情况下。 - 希望避免因多维分类而导致的大量子类爆炸问题。 --- ### 示例代码及解释 下面是一个完整的 C++ 示例,展示如何利用桥接模式将形状和颜色解耦。 ```cpp #include <iostream> using namespace std; // Implementor: 定义实现部分的接口 class Color { public: virtual void applyColor() const = 0; }; // ConcreteImplementorA: 蓝色实现 class Blue : public Color { public: void applyColor() const override { cout << "Applying blue color." << endl; } }; // ConcreteImplementorB: 红色实现 class Red : public Color { public: void applyColor() const override { cout << "Applying red color." << endl; } }; // Abstraction: 抽象部分接口 class Shape { protected: Color* _color; public: Shape(Color* c) : _color(c) {} virtual ~Shape() { delete _color; } virtual void draw() const = 0; }; // RefinedAbstraction: 圆形具体化 class Circle : public Shape { public: Circle(Color* c) : Shape(c) {} void draw() const override { cout << "Drawing a circle..." << endl; _color->applyColor(); } }; // RefinedAbstraction: 方形具体化 class Square : public Shape { public: Square(Color* c) : Shape(c) {} void draw() const override { cout << "Drawing a square..." << endl; _color->applyColor(); } }; int main() { // 创建蓝色圆形 Shape* shape1 = new Circle(new Blue()); shape1->draw(); // 创建红色方形 Shape* shape2 = new Square(new Red()); shape2->draw(); delete shape1; delete shape2; return 0; } ``` #### 输出结果 运行上述程序后,控制台将显示如下内容: ``` Drawing a circle... Applying blue color. Drawing a square... Applying red color. ``` --- ### 使用注意事项 1. **性能开销** 桥接模式可能带来额外的对象创建成本以及间接访问带来的效率损失。 2. **清晰的设计意图** 在使用前应明确哪些方面需要保持灵活性,从而合理分配职责给抽象层和实现层。 3. **过度设计的风险** 如果项目本身并不具备明显的多变维度,则不应盲目采用该模式以免造成不必要的复杂性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值