桥接设计模式

现实世界中的例子

假设你有一个包含不同页面的网站,并且你需要允许用户更改主题。你会怎么做?是为每个主题创建每个页面的多个副本,还是只创建单独的主题并根据用户的偏好加载它们?桥接模式允许你采用后者。

用简单的话来说

桥接模式是关于优先考虑组合而非继承的。它将实现细节从一个层次结构中提取出来,放入另一个对象层次结构中。

维基百科说

桥接模式是软件工程中使用的一种设计模式,旨在"将抽象部分与它的实现部分分离,使它们都可以独立地变化"。

程序示例

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

// Theme interface
class Theme {
public:
    virtual std::string getColor() const = 0;
    virtual ~Theme() = default;
};

// DarkTheme implementation
class DarkTheme : public Theme {
public:
    std::string getColor() const override {
        return "Dark Black";
    }
};

// LightTheme implementation
class LightTheme : public Theme {
public:
    std::string getColor() const override {
        return "Off white";
    }
};

// AquaTheme implementation
class AquaTheme : public Theme {
public:
    std::string getColor() const override {
        return "Light blue";
    }
};

// WebPage interface
class WebPage {
protected:
    std::shared_ptr<Theme> theme;
public:
    WebPage(std::shared_ptr<Theme> theme) : theme(theme) {}
    virtual std::string getContent() const = 0;
    virtual ~WebPage() = default;
};

// About page implementation
class About : public WebPage {
public:
    About(std::shared_ptr<Theme> theme) : WebPage(theme) {}

    std::string getContent() const override {
        return "About page in " + theme->getColor();
    }
};

// Careers page implementation
class Careers : public WebPage {
public:
    Careers(std::shared_ptr<Theme> theme) : WebPage(theme) {}

    std::string getContent() const override {
        return "Careers page in " + theme->getColor();
    }
};

int main() {
    std::shared_ptr<Theme> darkTheme = std::make_shared<DarkTheme>();

    std::shared_ptr<WebPage> about = std::make_shared<About>(darkTheme);
    std::shared_ptr<WebPage> careers = std::make_shared<Careers>(darkTheme);

    std::cout << about->getContent() << std::endl;   // "About page in Dark Black"
    std::cout << careers->getContent() << std::endl; // "Careers page in Dark Black"

    return 0;
}

解释:

  1. 主题接口和实现:定义了Theme接口,并提供了不同的主题实现(DarkTheme、LightTheme、AquaTheme)。每个实现提供不同的颜色主题。
  2. 网页接口和实现:定义了WebPage接口,并提供了具体的页面实现(About、Careers),这些实现使用主题来生成内容。
  3. 组合优于继承:我们使用组合来注入不同的主题到页面对象中,而不是为每个页面的每个主题创建多个类。
  4. 动态绑定:使用std::shared_ptr进行动态绑定,允许在运行时设置或更改主题。
相关推荐
孞㐑¥1 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
水木兰亭4 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
GodKeyNet5 小时前
设计模式-模板模式
设计模式·模板模式
CoderCodingNo5 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
秋风&萧瑟7 小时前
【C++】C++中的友元函数和友元类
c++
梁诚斌7 小时前
使用OpenSSL接口读取pem编码格式文件中的证书
开发语言·c++
缘来是庄8 小时前
设计模式之建造者模式
java·设计模式·建造者模式
铛铛啦啦啦11 小时前
“对象创建”模式之原型模式
设计模式·原型模式
2301_8035545211 小时前
c++中的绑定器
开发语言·c++·算法
海棠蚀omo11 小时前
C++笔记-位图和布隆过滤器
开发语言·c++·笔记