目录
桥接模式(Bridge Pattern)
桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化。桥接模式通过组合(而不是继承)来实现这种分离,从而提高了系统的灵活性和可扩展性。
实际应用
桥接模式的核心思想是将抽象与实现分离,这样两者可以独立变化。抽象部分定义高层接口,而实现部分则定义具体的实现。通过组合的方式,抽象部分持有实现部分的引用,从而将两者桥接起来。
图形绘制系统
假设我们有一个图形绘制系统,需要支持不同的平台(如Windows和Linux),以及不同的图形类型(如圆形和矩形)。
#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;
}
数据库连接器
假设我们有一个应用程序需要支持多种数据库(如MySQL和PostgreSQL),同时需要不同的查询接口(如简单查询和复杂查询)。
#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;
}
报告生成系统
假设我们有一个报告生成系统,需要支持不同的报告格式(如PDF和HTML),同时需要不同的数据源(如数据库和文件)。
#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;
}
总结
桥接模式通过分离抽象和实现,使得系统的扩展和维护更加灵活和方便,并且可以很容易地进行组合以适应不同的需求。