当前位置: 首页 > article >正文

C++中的桥接模式

桥接模式(Bridge Pattern)

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

实际应用

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

图形绘制系统

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

#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),同时需要不同的查询接口(如简单查询和复杂查询)。

#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),同时需要不同的数据源(如数据库和文件)。

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

总结

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


http://www.kler.cn/a/398855.html

相关文章:

  • 黑马智慧商城项目学习笔记
  • ChromeDriver 官方下载地址_测试自动化浏览器驱动
  • 023、ELK 从入门到实践
  • VMD + CEEMDAN 二次分解,CNN-LSTM预测模型
  • Python爬虫----python爬虫基础
  • HMI FUXA测试
  • 为什么要使用Ansible实现Linux管理自动化?
  • uniapp微信小程序接入airkiss插件进行WIFI配网
  • ODOO学习笔记(7):模块化架构(按需安装)
  • 基于Java Springboot宠物救助管理系统
  • jQuery UI 为什么使用部件库
  • 4.2 Android NDK 基础概念
  • 深入理解 Redis跳跃表 Skip List 原理|图解查询、插入
  • 使用 OpenAI 提高 Appium 测试脚本效率:从优化到跨平台支持
  • 人工智能之图像预处理、数据库、GUI布局的综合应用(数据库部分、GUI布局设计)
  • 微知-DOCA ARGP参数模块的相关接口和用法(config单元、params单元,argp pipe line,回调)
  • Nginx负载均衡示例
  • install与cp库文件和头文件差异
  • 用 React18 构建Tic-Tac-Toe(井字棋)游戏
  • 数据处理与统计分析——04-Pandas中Series的常用方法、属性、布尔索引、运算操作
  • 阿里斑马智行 2025届秋招 NLP算法工程师
  • The Internals of PostgreSQL 翻译版 持续更新...
  • Java语言在医疗健康小程序开发中的应用研究
  • 机器学习周报(11.11-11.17)
  • 力扣-2175、世界排名的变化
  • mybatis 动态SQL语句