装修公司怎么做网站推广,wordpress 模板修改,网站弹出信息怎么做,wordpress 添加锚点介绍 桥接模式将抽象部分与其实现部分分离#xff0c;使它们都可以独立地变化。它是一种对象结构型模式#xff0c;又称为柄体模式或接口模式
实现
myclass.h
//
// Created by yuwp on 2024/1/12.
//#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H#…介绍 桥接模式将抽象部分与其实现部分分离使它们都可以独立地变化。它是一种对象结构型模式又称为柄体模式或接口模式
实现
myclass.h
//
// Created by yuwp on 2024/1/12.
//#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H#include iostreamclass Implementor { // 实现抽象类例如颜料
public:virtual void operationImpl() 0;
};class Abstract { // 抽象类例如毛笔
public:virtual void operation() 0;
protected:Implementor *m_imp;
};class RefinedAbstraction : public Abstract { // 扩充抽象类例如大号毛笔
public:RefinedAbstraction(Implementor *imp);~RefinedAbstraction();void operation() override;
};class ConcreteImplementorA : public Implementor { // 具体实现类例如红色颜料
public:void operationImpl() override;
};#endif //DESIGNPATTERNS_MYCLASS_Hmyclass.cpp
//
// Created by yuwp on 2024/1/12.
//#include myclass.hRefinedAbstraction::RefinedAbstraction(Implementor *imp) {m_imp imp;
}RefinedAbstraction::~RefinedAbstraction() {}void RefinedAbstraction::operation() {if (m_imp) {m_imp-operationImpl();}std::cout RefinedAbstraction::operation() std::endl;
}void ConcreteImplementorA::operationImpl() {std::cout ConcreteImplementorA::operationImpl() std::endl;
}main.cpp
#include iostream
#include mutex
#include myclass.hint main() {Implementor *imp new ConcreteImplementorA();Abstract *ab new RefinedAbstraction(imp);ab-operation();return 0;
}
总结
优点 1. 分离抽象接口及其实现部分。桥接模式使用“对象间的关联关系”解耦了抽象和实现之间固有的绑定关系使得抽象和实现可以沿着各自的维度来变化即抽象和实现不再在同一个继承层次结构中而是“子类化”它们使它们各自都具有自己的子类以便任意组合子类从而获得多维度组合对象 2. 在很多情况下桥接模式可以取代多层继承方案。多层继承方案违背了单一职责原则复用性较差且类的个数非常多。桥接模式是比多层继承方案更好的解决方法它极大地减少了子类的个数。 3. 桥接模式提高了系统的可扩展性。在两个变化维度中任意扩展一个维度都不需要修改原有系统符合开闭原则。
缺点 1. 桥接模式的使用会增加系统的理解与设计难度。由于关联关系建立在抽象层要求开发者一开始就针对抽象层进行设计与编程。 2. 桥接模式要求正确识别出系统中两个独立变化的维度因此其使用范围具有一定的局限性如何正确识别两个独立维度也需要一定的经验积累。
适用场景 1. 如果一个系统需要在抽象类和具体类之间增加更多的灵活性避免在两个层次之间建立静态的继承关系通过桥接模式可以使它们在抽象层建立一个关联关系。 2. 抽象部分和实现部分可以以继承的方式独立扩展而互不影响在程序运行时可以动态地将一个抽象类子类的对象和一个实现类子类的对象进行组合即系统需要对抽象类角色和实现类角色进行动态耦合。 3. 一个类存在两个或多个独立变化的维度且这两个或多个维度都需要独立进行扩展。 4. 对于那些不希望使用继承或因为多层继承导致系统类的个数急剧增加的系统桥接模式尤为适用。
练习
myclass.h
//
// Created by yuwp on 2024/1/12.
//#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H#include iostreamclass FileFormat { // 实现抽象类
public:virtual void toFile(std::string data, std::string file) 0;
};class Database { // 抽象类
public:virtual void trans() 0;protected:FileFormat *m_fileFormat;
};class MysqlDatabase : public Database { // 扩充抽象类
public:MysqlDatabase(FileFormat *fileFormat);void trans() override;};class RedisDatabase : public Database {
public:RedisDatabase(FileFormat *fileFormat);void trans() override;protected:FileFormat *m_fileFormat;
};class TXTFileFormat : public FileFormat { // 具体实现类例如红色颜料
public:void toFile(std::string data, std::string file) override;
};class XMLFileFormat : public FileFormat {
public:void toFile(std::string data, std::string file) override;
};class PDFFileFormat : public FileFormat {
public:void toFile(std::string data, std::string file) override;
};#endif //DESIGNPATTERNS_MYCLASS_Hmyclass.cpp
//
// Created by yuwp on 2024/1/12.
//#include myclass.hMysqlDatabase::MysqlDatabase(FileFormat *fileFormat) {m_fileFormat fileFormat;
}void MysqlDatabase::trans() {std::string data mysql;if (m_fileFormat) {m_fileFormat-toFile(data, mysql);}
}RedisDatabase::RedisDatabase(FileFormat *fileFormat) {m_fileFormat fileFormat;
}void RedisDatabase::trans() {std::string data redis;if (m_fileFormat) {m_fileFormat-toFile(data, redis);}
}void TXTFileFormat::toFile(std::string data, std::string file) {std::cout \ data \ 转换为\ file .txt\ std::endl;
}void XMLFileFormat::toFile(std::string data, std::string file) {std::cout \ data \ 转换为\ file .xml\ std::endl;
}void PDFFileFormat::toFile(std::string data, std::string file) {std::cout \ data \ 转换为\ file .pdf\ std::endl;
}main.cpp
#include iostream
#include mutex
#include myclass.hint main() {FileFormat *txtFormat new TXTFileFormat();FileFormat *xmlFormat new XMLFileFormat();FileFormat *pdfFormat new PDFFileFormat();Database *database new MysqlDatabase(txtFormat);database-trans();delete database;database new MysqlDatabase(xmlFormat);database-trans();delete database;database new MysqlDatabase(pdfFormat);database-trans();delete database;database new RedisDatabase(txtFormat);database-trans();delete database;database new RedisDatabase(xmlFormat);database-trans();delete database;database new RedisDatabase(pdfFormat);database-trans();delete txtFormat;delete xmlFormat;delete pdfFormat;return 0;
}