桥接设计模式

现实世界中的例子

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

用简单的话来说

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

维基百科说

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

程序示例

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进行动态绑定,允许在运行时设置或更改主题。
相关推荐
不見星空36 分钟前
2025年第十六届蓝桥杯软件赛省赛C/C++大学A组个人解题
c语言·c++·蓝桥杯
jiunian_cn38 分钟前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
梁下轻语的秋缘1 小时前
每日c/c++题 备战蓝桥杯(洛谷P1387 最大正方形)
c语言·c++·蓝桥杯
熬夜学编程的小王1 小时前
【C++进阶篇】多态
c++·多态·静态绑定与动态绑定
UpUpUp……1 小时前
Linux--JsonCpp
linux·运维·服务器·c++·笔记·json
工藤新一¹2 小时前
蓝桥杯算法题 -蛇形矩阵(方向向量)
c++·算法·矩阵·蓝桥杯·方向向量
fallzzzzz3 小时前
C++ stl中的list的相关函数用法
c++·list
悦悦子a啊4 小时前
PTA:jmu-ds-最短路径
c++·算法·图论
小王努力学编程5 小时前
高并发内存池(三):TLS无锁访问以及Central Cache结构设计
jvm·数据结构·c++·学习
周努力.7 小时前
设计模式之中介者模式
设计模式·中介者模式