10结构型设计模式——桥接模式

#程序员如何平衡日常编码工作与提升式学习?#

一、桥接模式

桥接模式(Bridge Pattern)是结构型的设计模式之一。桥接模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来(解耦合),从而可以保持各部分的独立性以及应对它们的功能扩展。这个模式的关键在于提供一个桥梁,将抽象和具体的实现解耦,从而使得修改和扩展变得更为灵活。

桥接模式的结构图

桥接模式的组成

  1. 抽象类(Abstraction):定义高层次的操作接口,持有一个指向实现类的引用。
  2. 扩展抽象类(RefinedAbstraction):扩展抽象类的功能,但仍然依赖于抽象类定义的接口。
  3. 实现接口(Implementor):定义实现的接口,通常包括一些基本操作,不包括抽象层的操作。
  4. 具体实现类(ConcreteImplementor):实现Implementor(实现接口)的具体操作。

桥接模式的优点

  • 分离抽象与实现:允许在不影响抽象接口的情况下改变实现。
  • 增加灵活性:可以独立地扩展抽象和实现部分。
  • 提高可维护性:通过将功能分离,减少了代码的耦合性,使得维护和扩展更加容易。

二、桥接模式的应用场景 

  1. 需要将抽象和实现分离:当你有一个系统需要同时支持多个维度的变化,比如不同的抽象层次和不同的实现方式,桥接模式能够有效地将这两个维度解耦,使得修改和扩展变得更灵活。

  2. 避免在每个变化点创建大量的子类:如果一个系统中存在多个抽象层次和实现方式,直接继承组合会导致大量的子类产生。桥接模式通过分离抽象和实现,可以避免这种情况,使得系统结构更加简洁。

  3. 系统可能需要动态切换实现:当系统在运行时需要动态选择或切换不同的实现时,桥接模式提供了一种灵活的方式来管理这些实现,从而减少系统的复杂度。

  4. 需要提高系统的可扩展性:桥接模式允许在不影响其他部分的情况下独立地扩展抽象和实现部分。这对于希望在将来轻松地添加新的功能或实现的系统特别有用。

  5. 复杂的对象结构需要灵活的管理:当对象的结构非常复杂,例如,涉及到多个维度的变化时,桥接模式可以将这些维度分开管理,从而提高系统的可维护性和灵活性。

具体应用示例

  1. 图形绘制系统:在绘制不同类型的图形(如圆形、方形等)时,可以将图形的抽象(如如何绘制图形)和具体的绘制实现(如不同的绘图API)分开处理。例如,你可以使用桥接模式来支持不同的绘图API(如OpenGL、DirectX)和不同的图形(如圆形、矩形)组合。

  2. 用户界面(UI)框架:UI框架通常需要支持多个平台(如Windows、macOS、Linux)的具体实现,同时又要保持统一的用户界面设计。桥接模式可以将UI组件的抽象层(如按钮、文本框)与具体的平台实现分离开来,从而使得不同平台上的UI组件可以共享相同的抽象层。

  3. 跨平台开发:在需要跨多个操作系统或硬件平台的应用程序中,桥接模式可以用来将平台特定的实现与应用程序的核心逻辑分开,从而简化跨平台的开发和维护工作。

  4. 持久化层和数据库:当一个系统需要支持多种数据库(如MySQL、PostgreSQL、MongoDB)时,可以使用桥接模式将持久化层的抽象接口与具体的数据库实现分开,从而使得系统能够在不修改持久化逻辑的情况下,轻松切换或扩展数据库支持。

三、桥接模式的设计方法

 bridge.cpp

#include <iostream>
#include <string>

// 定义平台接口
class DrawingAPI {
public:
    virtual void drawButton(const std::string& label, int x, int y, int width, int height) = 0;
    virtual void drawTextBox(const std::string& text, int x, int y, int width, int height) = 0;
    virtual ~DrawingAPI() = default;
};

// 定义具体平台实现
class WindowsAPI : public DrawingAPI {
public:
    void drawButton(const std::string& label, int x, int y, int width, int height) override {
        std::cout << "Windows: Drawing button '" << label << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }

    void drawTextBox(const std::string& text, int x, int y, int width, int height) override {
        std::cout << "Windows: Drawing text box with text '" << text << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }
};

class MacOSAPI : public DrawingAPI {
public:
    void drawButton(const std::string& label, int x, int y, int width, int height) override {
        std::cout << "MacOS: Drawing button '" << label << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }

    void drawTextBox(const std::string& text, int x, int y, int width, int height) override {
        std::cout << "MacOS: Drawing text box with text '" << text << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }
};

class LinuxAPI : public DrawingAPI {
public:
    void drawButton(const std::string& label, int x, int y, int width, int height) override {
        std::cout << "Linux: Drawing button '" << label << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }

    void drawTextBox(const std::string& text, int x, int y, int width, int height) override {
        std::cout << "Linux: Drawing text box with text '" << text << "' at (" << x << ", " << y << ") with size " << width << "x" << height << std::endl;
    }
};

// 定义UI组件的抽象层
class UIComponent {
public:
    UIComponent(DrawingAPI* api) : api_(api) {}

    virtual void draw() = 0;

protected:
    DrawingAPI* api_;
};

// 定义具体的UI组件
class Button : public UIComponent {
public:
    Button(const std::string& label, int x, int y, int width, int height, DrawingAPI* api)
        : UIComponent(api), label_(label), x_(x), y_(y), width_(width), height_(height) {}

    void draw() override {
        api_->drawButton(label_, x_, y_, width_, height_);
    }

private:
    std::string label_;
    int x_, y_, width_, height_;
};

class TextBox : public UIComponent {
public:
    TextBox(const std::string& text, int x, int y, int width, int height, DrawingAPI* api)
        : UIComponent(api), text_(text), x_(x), y_(y), width_(width), height_(height) {}

    void draw() override {
        api_->drawTextBox(text_, x_, y_, width_, height_);
    }

private:
    std::string text_;
    int x_, y_, width_, height_;
};


void doWorking() {
    WindowsAPI windowsAPI;
    MacOSAPI macOSAPI;
    LinuxAPI linuxAPI;

    Button windowsButton("Submit", 100, 50, 200, 40, &windowsAPI);
    TextBox windowsTextBox("Enter text", 100, 100, 300, 40, &windowsAPI);

    Button macButton("Submit", 100, 50, 200, 40, &macOSAPI);
    TextBox macTextBox("Enter text", 100, 100, 300, 40, &macOSAPI);
    
    Button linuxButton("Submit", 100, 50, 200, 40, &linuxAPI);
    TextBox linuxTextBox("Enter text", 100, 100, 300, 40, &linuxAPI);

    std::cout << "Drawing Windows UI components:" << std::endl;
    windowsButton.draw();
    windowsTextBox.draw();

    std::cout << "\nDrawing MacOS UI components:" << std::endl;
    macButton.draw();
    macTextBox.draw();
    
    std::cout << "\nDrawing Linux UI components:" << std::endl;
    linuxButton.draw();
    linuxTextBox.draw();
    
    return ;
}

int main() {
	// start working
	doWorking();
	return 0;
}

运行效果

四、总结

在这个示例中:

  • DrawingAPI 是平台接口,定义了绘制方法。
  • WindowsAPI 和 MacOSAPI 、LinuxAPI 是具体实现,提供平台特定的绘制方法。
  • UIComponent 是抽象层,持有DrawingAPI 的引用。
  • Button 和 TextBox 是具体的UI组件,实现了 UIComponent 。

桥接模式通过分离抽象和实现,使得UI组件的抽象层可以与不同的平台实现解耦,从而使得支持新平台或修改现有平台的实现变得更加容易。

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值