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;
}

总结

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

相关推荐
捕鲸叉10 分钟前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer15 分钟前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq17 分钟前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
hairenjing11231 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
记录成长java2 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山2 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
青花瓷2 小时前
C++__XCode工程中Debug版本库向Release版本库的切换
c++·xcode
睡觉谁叫~~~2 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
音徽编程2 小时前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
观音山保我别报错2 小时前
C语言扫雷小游戏
c语言·开发语言·算法