C++中的桥接模式

目录

[桥接模式(Bridge Pattern)](#桥接模式(Bridge Pattern))

实际应用

图形绘制系统

数据库连接器

报告生成系统

总结


桥接模式(Bridge Pattern)

桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化。桥接模式通过组合(而不是继承)来实现这种分离,从而提高了系统的灵活性和可扩展性。

实际应用

桥接模式的核心思想是将抽象与实现分离,这样两者可以独立变化。抽象部分定义高层接口,而实现部分则定义具体的实现。通过组合的方式,抽象部分持有实现部分的引用,从而将两者桥接起来。

图形绘制系统

假设我们有一个图形绘制系统,需要支持不同的平台(如WindowsLinux),以及不同的图形类型(如圆形和矩形)。

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

// 实现接口
class DrawingAPI {
public:
    virtual ~DrawingAPI() = default;
    virtual void drawCircle(double x, double y, double radius) const = 0;
    virtual void drawRectangle(double x, double y, double width, double height) const = 0;
};

// 具体实现:Windows绘制API
class WindowsDrawingAPI : public DrawingAPI {
public:
    void drawCircle(double x, double y, double radius) const override {
        std::cout << "Windows Drawing API: Drawing Circle at (" << x << ", " << y << ") with radius " << radius << "\n";
    }
    void drawRectangle(double x, double y, double width, double height) const override {
        std::cout << "Windows Drawing API: Drawing Rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << "\n";
    }
};

// 具体实现:Linux绘制API
class LinuxDrawingAPI : public DrawingAPI {
public:
    void drawCircle(double x, double y, double radius) const override {
        std::cout << "Linux Drawing API: Drawing Circle at (" << x << ", " << y << ") with radius " << radius << "\n";
    }
    void drawRectangle(double x, double y, double width, double height) const override {
        std::cout << "Linux Drawing API: Drawing Rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << "\n";
    }
};

// 抽象接口
class Shape {
protected:
    std::shared_ptr<DrawingAPI> drawingAPI;
public:
    Shape(std::shared_ptr<DrawingAPI> api) : drawingAPI(api) {}
    virtual ~Shape() = default;
    virtual void draw() const = 0;
};

// 具体抽象:圆形
class Circle : public Shape {
private:
    double x, y, radius;
public:
    Circle(double x, double y, double radius, std::shared_ptr<DrawingAPI> api) 
        : Shape(api), x(x), y(y), radius(radius) {}

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

// 具体抽象:矩形
class Rectangle : public Shape {
private:
    double x, y, width, height;
public:
    Rectangle(double x, double y, double width, double height, std::shared_ptr<DrawingAPI> api) 
        : Shape(api), x(x), y(y), width(width), height(height) {}

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

int main() {
    std::shared_ptr<DrawingAPI> windowsAPI = std::make_shared<WindowsDrawingAPI>();
    std::shared_ptr<DrawingAPI> linuxAPI = std::make_shared<LinuxDrawingAPI>();

    Circle circle1(10, 20, 5, windowsAPI);
    Rectangle rectangle1(30, 40, 15, 10, linuxAPI);

    circle1.draw();
    rectangle1.draw();

    return 0;
}

数据库连接器

假设我们有一个应用程序需要支持多种数据库(如MySQLPostgreSQL),同时需要不同的查询接口(如简单查询和复杂查询)。

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

// 实现接口
class DBConnector {
public:
    virtual ~DBConnector() = default;
    virtual void connect() const = 0;
    virtual void executeQuery(const std::string& query) const = 0;
};

// 具体实现:MySQL连接器
class MySQLConnector : public DBConnector {
public:
    void connect() const override {
        std::cout << "Connecting to MySQL database.\n";
    }
    void executeQuery(const std::string& query) const override {
        std::cout << "Executing MySQL query: " << query << "\n";
    }
};

// 具体实现:PostgreSQL连接器
class PostgreSQLConnector : public DBConnector {
public:
    void connect() const override {
        std::cout << "Connecting to PostgreSQL database.\n";
    }
    void executeQuery(const std::string& query) const override {
        std::cout << "Executing PostgreSQL query: " << query << "\n";
    }
};

// 抽象接口
class QueryExecutor {
protected:
    std::shared_ptr<DBConnector> dbConnector;
public:
    QueryExecutor(std::shared_ptr<DBConnector> connector) : dbConnector(connector) {}
    virtual ~QueryExecutor() = default;
    virtual void execute(const std::string& query) const = 0;
};

// 具体抽象:简单查询执行器
class SimpleQueryExecutor : public QueryExecutor {
public:
    SimpleQueryExecutor(std::shared_ptr<DBConnector> connector) : QueryExecutor(connector) {}

    void execute(const std::string& query) const override {
        dbConnector->connect();
        dbConnector->executeQuery(query);
    }
};

// 具体抽象:复杂查询执行器
class ComplexQueryExecutor : public QueryExecutor {
public:
    ComplexQueryExecutor(std::shared_ptr<DBConnector> connector) : QueryExecutor(connector) {}

    void execute(const std::string& query) const override {
        dbConnector->connect();
        dbConnector->executeQuery("BEGIN TRANSACTION");
        dbConnector->executeQuery(query);
        dbConnector->executeQuery("END TRANSACTION");
    }
};

int main() {
    std::shared_ptr<DBConnector> mysqlConnector = std::make_shared<MySQLConnector>();
    std::shared_ptr<DBConnector> postgresConnector = std::make_shared<PostgreSQLConnector>();

    SimpleQueryExecutor simpleMySQLExecutor(mysqlConnector);
    ComplexQueryExecutor complexPostgresExecutor(postgresConnector);

    simpleMySQLExecutor.execute("SELECT * FROM users");
    complexPostgresExecutor.execute("UPDATE users SET name = 'John' WHERE id = 1");

    return 0;
}

报告生成系统

假设我们有一个报告生成系统,需要支持不同的报告格式(如PDFHTML),同时需要不同的数据源(如数据库和文件)。

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

// 实现接口
class ReportFormat {
public:
    virtual ~ReportFormat() = default;
    virtual void generateReport(const std::string& content) const = 0;
};

// 具体实现:PDF报告格式
class PDFReportFormat : public ReportFormat {
public:
    void generateReport(const std::string& content) const override {
        std::cout << "Generating PDF report with content: " << content << "\n";
    }
};

// 具体实现:HTML报告格式
class HTMLReportFormat : public ReportFormat {
public:
    void generateReport(const std::string& content) const override {
        std::cout << "Generating HTML report with content: " << content << "\n";
    }
};

// 抽象接口
class Report {
protected:
    std::shared_ptr<ReportFormat> reportFormat;
public:
    Report(std::shared_ptr<ReportFormat> format) : reportFormat(format) {}
    virtual ~Report() = default;
    virtual void generate() const = 0;
};

// 具体抽象:数据库报告
class DatabaseReport : public Report {
private:
    std::string data;
public:
    DatabaseReport(const std::string& data, std::shared_ptr<ReportFormat> format) 
        : Report(format), data(data) {}

    void generate() const override {
        reportFormat->generateReport("Database Report: " + data);
    }
};

// 具体抽象:文件报告
class FileReport : public Report {
private:
    std::string data;
public:
    FileReport(const std::string& data, std::shared_ptr<ReportFormat> format) 
        : Report(format), data(data) {}

    void generate() const override {
        reportFormat->generateReport("File Report: " + data);
    }
};

int main() {
    std::shared_ptr<ReportFormat> pdfFormat = std::make_shared<PDFReportFormat>();
    std::shared_ptr<ReportFormat> htmlFormat = std::make_shared<HTMLReportFormat>();

    DatabaseReport dbReport("User data from DB", pdfFormat);
    FileReport fileReport("User data from File", htmlFormat);

    dbReport.generate();
    fileReport.generate();

    return 0;
}

总结

桥接模式通过分离抽象和实现,使得系统的扩展和维护更加灵活和方便,并且可以很容易地进行组合以适应不同的需求。

相关推荐
wenxin-36 分钟前
NS3网络模拟器中如何利用Gnuplot工具像MATLAB一样绘制各类图形?
开发语言·matlab·画图·ns3·lr-wpan
数据小爬虫@3 小时前
深入解析:使用 Python 爬虫获取苏宁商品详情
开发语言·爬虫·python
健胃消食片片片片3 小时前
Python爬虫技术:高效数据收集与深度挖掘
开发语言·爬虫·python
王老师青少年编程4 小时前
gesp(C++五级)(14)洛谷:B4071:[GESP202412 五级] 武器强化
开发语言·c++·算法·gesp·csp·信奥赛
DogDaoDao4 小时前
leetcode 面试经典 150 题:有效的括号
c++·算法·leetcode·面试··stack·有效的括号
一只小bit5 小时前
C++之初识模版
开发语言·c++
雯宝5 小时前
STM32 GPIO工作模式
stm32·单片机·嵌入式硬件
王磊鑫5 小时前
C语言小项目——通讯录
c语言·开发语言
钢铁男儿5 小时前
C# 委托和事件(事件)
开发语言·c#
Ai 编码助手6 小时前
在 Go 语言中如何高效地处理集合
开发语言·后端·golang