《桥接模式(极简c++)》

        本文章属于专栏《设计模式(极简c++版)》


         继续上一篇《原型模式(极简c++)》。本章简要说明桥接模式。本文分为模式说明、本质思想、实践建议、代码示例四个部分。

  • 模式说明
    • 方案: 将抽象部分与它的实现部分分离,使多个组合在一起的品类可以独立变化。
    • 优点:
      • 分离抽象和实现部分,使得它们可以独立地变化,易于扩展。
      • 通过对象组合而不是继承的方式实现解耦,提高了代码的灵活性。
    • 缺点:
      • 增加了系统的复杂度,因为需要多个抽象类和实现类。
  • 本质思想:用一个抽象类组合另外一个抽象类,如形状和颜色,在构建时,使用具体的类完成组合,如红色的正方形。
  • 实践建议:一个基础主体包含多个属性时,都可以使用。该模式能很好地保留设计时的模块语义,方便未来维护
#include <iostream>

// 实现部分接口:颜色
class Color {
public:
    virtual void applyColor() const = 0;
};

// 具体实现类:红色
class RedColor : public Color {
public:
    void applyColor() const override {
        std::cout << "Red" << std::endl;
    }
};

// 具体实现类:绿色
class GreenColor : public Color {
public:
    void applyColor() const override {
        std::cout << "Green" << std::endl;
    }
};

// 抽象部分类:形状
class Shape {
protected:
    Color* color;

public:
    Shape(Color* c) : color(c) {}

    virtual void draw() const = 0;
};

// 扩展的抽象部分类:圆形
class Circle : public Shape {
public:
    Circle(Color* c) : Shape(c) {}

    void draw() const override {
        std::cout << "Drawing Circle with color ";
        color->applyColor();
    }
};

// 扩展的抽象部分类:矩形
class Rectangle : public Shape {
public:
    Rectangle(Color* c) : Shape(c) {}

    void draw() const override {
        std::cout << "Drawing Rectangle with color ";
        color->applyColor();
    }
};

int main() {
    Color* red = new RedColor();
    Color* green = new GreenColor();

    Shape* redCircle = new Circle(red);
    Shape* greenRectangle = new Rectangle(green);

    redCircle->draw(); // 输出: Drawing Circle with color Red
    greenRectangle->draw(); // 输出: Drawing Rectangle with color Green

    delete red;
    delete green;
    delete redCircle;
    delete greenRectangle;

    return 0;
}

相关推荐

  1. 模式c++)》

    2024-03-23 21:44:04       39 阅读
  2. 模版模式c++)》

    2024-03-23 21:44:04       34 阅读
  3. 《工厂模式c++)》

    2024-03-23 21:44:04       42 阅读
  4. 《原型模式c++)》

    2024-03-23 21:44:04       57 阅读
  5. 《过滤器模式c++)》

    2024-03-23 21:44:04       46 阅读
  6. 《适配器模式c++)》

    2024-03-23 21:44:04       45 阅读
  7. 《组合模式c++)》

    2024-03-23 21:44:04       42 阅读
  8. 《外观模式c++)》

    2024-03-23 21:44:04       38 阅读
  9. 《备忘录模式c++)》

    2024-03-23 21:44:04       37 阅读
  10. 《命令模式c++)》

    2024-03-23 21:44:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 21:44:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 21:44:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 21:44:04       87 阅读
  4. Python语言-面向对象

    2024-03-23 21:44:04       96 阅读

热门阅读

  1. dubbo RandomLoadBalance的一点优化

    2024-03-23 21:44:04       39 阅读
  2. 面试算法-43-最长递增子序列

    2024-03-23 21:44:04       40 阅读
  3. 使用VisualStudio集成开发nodejs的addon项目

    2024-03-23 21:44:04       40 阅读
  4. 【Docker】Docker官方发布26.0.0社区版

    2024-03-23 21:44:04       33 阅读
  5. Docker 安装 CentOS7 系统

    2024-03-23 21:44:04       37 阅读
  6. 实验7-2-10 简易连连看(PTA)

    2024-03-23 21:44:04       34 阅读
  7. RESTful架构

    2024-03-23 21:44:04       38 阅读
  8. Vue复习

    Vue复习

    2024-03-23 21:44:04      37 阅读
  9. go 基础中的一些坑(2)

    2024-03-23 21:44:04       35 阅读
  10. 氧化铝电容的工艺结构原理及选型参数总结

    2024-03-23 21:44:04       41 阅读
  11. Python之functools模块之lru_cache

    2024-03-23 21:44:04       43 阅读
  12. 智能新纪元:AI大模型学习的奥秘与挑战

    2024-03-23 21:44:04       40 阅读
  13. 274. H 指数

    2024-03-23 21:44:04       35 阅读
  14. VMware Workstation17安装

    2024-03-23 21:44:04       45 阅读