【C++设计模式】结构型模式:桥接模式

结构型模式:桥接模式

假设你有一个绘图程序,需要支持绘制多种颜色的多种形状。如果直接将 n n n种形状和 m m m个颜色结合在一起,会导致类的数量急剧增加( n × m n\times m n×m个)。比如,3种形状(如圆形、矩形和三角形),3种颜色(如红色、蓝色和绿色)就需要9个类:

cpp 复制代码
#include <iostream>

// 形状类(圆形)
class RedCircle {
public:
    void draw() {
        std::cout << "Drawing Red Circle\n";
    }
};

class BlueCircle {
public:
    void draw() {
        std::cout << "Drawing Blue Circle\n";
    }
};

class GreenCircle {
public:
    void draw() {
        std::cout << "Drawing Green Circle\n";
    }
};

// 形状类(矩形)
class RedRectangle {
public:
    void draw() {
        std::cout << "Drawing Red Rectangle\n";
    }
};

class BlueRectangle {
public:
    void draw() {
        std::cout << "Drawing Blue Rectangle\n";
    }
};

class GreenRectangle {
public:
    void draw() {
        std::cout << "Drawing Green Rectangle\n";
    }
};

// 形状类(三角形)
class RedTriangle {
public:
    void draw() {
        std::cout << "Drawing Red Triangle\n";
    }
};

class BlueTriangle {
public:
    void draw() {
        std::cout << "Drawing Blue Triangle\n";
    }
};

class GreenTriangle {
public:
    void draw() {
        std::cout << "Drawing Green Triangle\n";
    }
};

// 客户端代码
int main() {
    // 创建不同颜色的形状组合
    RedCircle redCircle;
    BlueCircle blueCircle;
    GreenCircle greenCircle;

    RedRectangle redRectangle;
    BlueRectangle blueRectangle;
    GreenRectangle greenRectangle;

    RedTriangle redTriangle;
    BlueTriangle blueTriangle;
    GreenTriangle greenTriangle;

    // 绘制形状
    redCircle.draw();         // 输出: Drawing Red Circle
    blueCircle.draw();        // 输出: Drawing Blue Circle
    greenCircle.draw();       // 输出: Drawing Green Circle

    redRectangle.draw();      // 输出: Drawing Red Rectangle
    blueRectangle.draw();     // 输出: Drawing Blue Rectangle
    greenRectangle.draw();    // 输出: Drawing Green Rectangle

    redTriangle.draw();       // 输出: Drawing Red Triangle
    blueTriangle.draw();      // 输出: Drawing Blue Triangle
    greenTriangle.draw();     // 输出: Drawing Green Triangle

    return 0;
}

桥接模式将"高层次的接口或类"与"低层次的接口或类"分离 (...形状有...颜色,形状比颜色高一层),使得系统具有更好的灵活性和可扩展性。

下面使用桥接模式改写上文的代码:

cpp 复制代码
#include <iostream>
#include <memory>

// 实现层次:颜色接口
class Color {
public:
    virtual void fill() = 0;
    virtual ~Color() = default;
};

// 具体颜色类
class Red : public Color {
public:
    void fill() override {
        std::cout << "Filling with Red color\n";
    }
};

class Blue : public Color {
public:
    void fill() override {
        std::cout << "Filling with Blue color\n";
    }
};

class Green : public Color {
public:
    void fill() override {
        std::cout << "Filling with Green color\n";
    }
};

// 抽象类:形状
class Shape {
protected:
    std::unique_ptr<Color> color;

public:
    Shape(std::unique_ptr<Color> c) : color(std::move(c)) {}
    virtual void draw() = 0;
    virtual ~Shape() = default;
};

// 具体形状类
class Circle : public Shape {
public:
    Circle(std::unique_ptr<Color> c) : Shape(std::move(c)) {}

    void draw() override {
        color->fill();
        std::cout << "Drawing Circle\n";
    }
};

class Rectangle : public Shape {
public:
    Rectangle(std::unique_ptr<Color> c) : Shape(std::move(c)) {}

    void draw() override {
        color->fill();
        std::cout << "Drawing Rectangle\n";
    }
};

class Triangle : public Shape {
public:
    Triangle(std::unique_ptr<Color> c) : Shape(std::move(c)) {}

    void draw() override {
        color->fill();
        std::cout << "Drawing Triangle\n";
    }
};

// 客户端代码
int main() {
    // 创建不同颜色的形状组合
    std::unique_ptr<Shape> shapes[9];

    shapes[0] = std::make_unique<Circle>(std::make_unique<Red>());
    shapes[1] = std::make_unique<Circle>(std::make_unique<Blue>());
    shapes[2] = std::make_unique<Circle>(std::make_unique<Green>());

    shapes[3] = std::make_unique<Rectangle>(std::make_unique<Red>());
    shapes[4] = std::make_unique<Rectangle>(std::make_unique<Blue>());
    shapes[5] = std::make_unique<Rectangle>(std::make_unique<Green>());

    shapes[6] = std::make_unique<Triangle>(std::make_unique<Red>());
    shapes[7] = std::make_unique<Triangle>(std::make_unique<Blue>());
    shapes[8] = std::make_unique<Triangle>(std::make_unique<Green>());

    // 绘制形状
    for (const auto& shape : shapes) {
        shape->draw();
    }

    return 0;
}

在没有使用桥接模式的情况下,每种形状和颜色组合都需要一个独立的类,随着形状和颜色的增加,类的数量会迅速膨胀。通过桥接模式,你只需为形状和颜色分别定义类,这大大减少了需要实现的类的数量。

相关推荐
王老师青少年编程6 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
初願致夕霞6 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
小小de风呀7 小时前
de风——【从零开始学C++】(五):内存管理
开发语言·c++
CHANG_THE_WORLD9 小时前
C语言中的 %*s 和 %.*s 和C++的字符串格式化输出
c语言·c++·c#
Supersist9 小时前
【设计模式03】使用模版模式+责任链模式优化实战
后端·设计模式·代码规范
螺丝钉的扭矩一瞬间产生高能蛋白10 小时前
QT的C++接口基础用法
c++·qt·嵌入式软件·嵌入式linux·linux应用
智者知已应修善业10 小时前
【51单片机模拟生日蜡烛】2023-10-10
c++·经验分享·笔记·算法·51单片机
智者知已应修善业10 小时前
【51单片机如何让LED灯从一亮到八,再从八亮到一】2023-10-13
c++·经验分享·笔记·算法·51单片机
qeen8710 小时前
【数据结构】二叉树相关经典函数C语言实现
c语言·数据结构·c++·笔记·学习·算法·二叉树
geovindu10 小时前
go: Interpreter Pattern
开发语言·设计模式·golang·解释器模式