桥接模式
image20250322130209019.png

有个抽象类燕窝,它有两个派生类燕盏燕窝和糖水燕窝。目前提供两种销售方式,即线下销售和电商专供,那就得按下面的方式去组合:

image20250322130334507.png

此后,如果你新增销售方式,你就得改动抽象类燕窝的派生类的代码,每新增一次,你就得改动一次。

桥接模式可以解决这个问题,那就是把销售方式也抽象出一个基类,燕窝派生类就只需要包含这个销售方式的抽象基类指针即可,不管往后的销售方式增加多少种。

9b0a4f3b62c956049e4361a40064f3e6.png

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 抽象类 Shape
class Shape {
protected:
std::shared_ptr<DrawAPI> drawAPI;
public:
Shape(std::shared_ptr<DrawAPI> api) : drawAPI(std::move(api)) {}
virtual void draw() = 0;
virtual ~Shape() = default;
};

// 具体的形状:圆形
class Circle : public Shape {
private:
int x, y, radius;
public:
Circle(int x, int y, int radius, std::shared_ptr<DrawAPI> api)
: Shape(std::move(api)), x(x), y(y), radius(radius) {}

void draw() override {
drawAPI->drawCircle(x, y, radius);
}
};

// 具体的形状:矩形
class Rectangle : public Shape {
private:
int x, y, width, height;
public:
Rectangle(int x, int y, int width, int height, std::shared_ptr<DrawAPI> api)
: Shape(std::move(api)), x(x), y(y), width(width), height(height) {}

void draw() override {
drawAPI->drawRectangle(x, y, width, height);
}
};

 

不同的图形有不同的颜色的画笔,将这些不同的颜色的画笔抽象出 DrawAPI,往后如果新增其他颜色,就只需要改动这边的代码,而无需改动 Shape 那边的相关代码。

从现象来看,是把改动的任务都给到 DrawAPI 这边了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 实现部分接口(DrawAPI)
class DrawAPI {
public:
virtual void drawCircle(int x, int y, int radius) = 0;
virtual void drawRectangle(int x, int y, int width, int height) = 0;
virtual ~DrawAPI() = default;
};

// 具体实现 A(红色绘制)
class RedShape : public DrawAPI {
public:
void drawCircle(int x, int y, int radius) override {
std::cout << "Drawing Circle [Color: Red, Radius: " << radius
<< ", x: " << x << ", y: " << y << "]\n";
}
void drawRectangle(int x, int y, int width, int height) override {
std::cout << "Drawing Rectangle [Color: Red, Width: " << width
<< ", Height: " << height << ", x: " << x << ", y: " << y << "]\n";
}
};

// 具体实现 B(绿色绘制)
class GreenShape : public DrawAPI {
public:
void drawCircle(int x, int y, int radius) override {
std::cout << "Drawing Circle [Color: Green, Radius: " << radius
<< ", x: " << x << ", y: " << y << "]\n";
}
void drawRectangle(int x, int y, int width, int height) override {
std::cout << "Drawing Rectangle [Color: Green, Width: " << width
<< ", Height: " << height << ", x: " << x << ", y: " << y << "]\n";
}
};

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main() {
// 创建红色绘制 API
std::shared_ptr<DrawAPI> redShapeAPI = std::make_shared<RedShape>();

// 创建绿色绘制 API
std::shared_ptr<DrawAPI> greenShapeAPI = std::make_shared<GreenShape>();

// 创建红色的圆
std::shared_ptr<Shape> redCircle = std::make_shared<Circle>(10, 10, 5, redShapeAPI);
redCircle->draw();

// 创建绿色的圆
std::shared_ptr<Shape> greenCircle = std::make_shared<Circle>(20, 20, 10, greenShapeAPI);
greenCircle->draw();

// 创建红色的矩形
std::shared_ptr<Shape> redRectangle = std::make_shared<Rectangle>(5, 5, 15, 10, redShapeAPI);
redRectangle->draw();

// 创建绿色的矩形
std::shared_ptr<Shape> greenRectangle = std::make_shared<Rectangle>(25, 25, 20, 15, greenShapeAPI);
greenRectangle->draw();

return 0;
}