[设计模式] 7 桥接模式 bridge
#include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp(); virtual void Operation() = 0; protected: AbstractionImp(); private: }; class Abstraction { public: virtual ~Abstraction(); virtual void Operation() = 0; protected: Abstraction(); private: }; class RefinedAbstraction:public Abstraction { public: RefinedAbstraction(AbstractionImp* imp); ~RefinedAbstraction(); void Operation(); protected: private: AbstractionImp* _imp; }; Abstraction::Abstraction() { } Abstraction::~Abstraction() { } RefinedAbstraction::RefinedAbstraction(AbstractionImp* imp) { _imp = imp; } RefinedAbstraction::~RefinedAbstraction() { } void RefinedAbstraction::Operation() { _imp->Operation(); } class ConcreteAbstractionImpA:public AbstractionImp { public: ConcreteAbstractionImpA(); ~ConcreteAbstractionImpA(); virtual void Operation(); protected: private: }; class ConcreteAbstractionImpB:public AbstractionImp { public: ConcreteAbstractionImpB(); ~ConcreteAbstractionImpB(); virtual void Operation(); protected: private: }; AbstractionImp::AbstractionImp() { } AbstractionImp::~AbstractionImp() { } void AbstractionImp::Operation() { cout<<"AbstractionImp....imp..."<<endl; } ConcreteAbstractionImpA::ConcreteAbstractionImpA() { } ConcreteAbstractionImpA::~ConcreteAbstractionImpA() { } void ConcreteAbstractionImpA::Operation() { cout<<"ConcreteAbstractionImpA...."<<endl; } ConcreteAbstractionImpB::ConcreteAbstractionImpB() { } ConcreteAbstractionImpB::~ConcreteAbstractionImpB() { } void ConcreteAbstractionImpB::Operation() { cout<<"ConcreteAbstractionImpB...."<<endl; } int main(int argc,char* argv[]) { AbstractionImp* imp = new ConcreteAbstractionImpA(); Abstraction* abs = new RefinedAbstraction(imp); abs->Operation(); imp = new ConcreteAbstractionImpB(); abs = new RefinedAbstraction(imp); abs->Operation(); return 0; }
输出:
ConcreteAbstractionImpA....
ConcreteAbstractionImpB....
[DP]书上定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化。考虑装操作系统,有多种配置的计算机,同样也有多款操作系统。如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展,减少它们的耦合度。当然了,两者之间有标准的接口。这样设计,不论是对于计算机,还是操作系统都是非常有利的。下面给出这种设计的UML图,其实就是桥接模式的UML图。
给出C++的一种实现:
#include<iostream> using namespace std; class OS { public: virtual void InstallOS_Imp() {} }; class WindowOS: public OS { public: void InstallOS_Imp() { cout<<"Install window OS"<<endl; } }; class LinuxOS: public OS { public: void InstallOS_Imp() { cout<<"Install Linux OS"<<endl; } }; class UnixOS: public OS { public: void InstallOS_Imp() { cout<<"Install UnixOS" <<endl; } }; class Computer { public: virtual void InstallOS(OS *os) {} }; class DellComputer: public Computer { public: void InstallOS(OS *os) { os->InstallOS_Imp(); } }; class AppleComputer: public Computer { public: void InstallOS(OS *os) { os->InstallOS_Imp(); } }; class HPComputer: public Computer { public: void InstallOS(OS *os) { os->InstallOS_Imp(); } }; int main() { OS *os1 = new WindowOS(); OS *os2 = new LinuxOS(); Computer *computer1 = new AppleComputer(); Computer *computer2 = new HPComputer(); computer1->InstallOS(os1); computer2->InstallOS(os2); }
输出:
Install window OS
Install Linux OS
Bridge 是设计模式中比较复杂和难理解的模式之一,也是 OO 开发与设计中经常会用到
的模式之一。使用组合(委托)的方式将抽象和实现彻底地解耦,这样的好处是抽象和实现
可以分别独立地变化,系统的耦合性也得到了很好的降低。
GoF 在说明 Bridge 模式时,在意图中指出 Bridge 模式“将抽象部分与它的实现部分分
离,使得它们可以独立地变化”。
实际上上面使用 Bridge 模式和使用带来问题方式的解决方案的根本区别在于是通过继
承还是通过组合的方式去实现一个功能需求。因此面向对象分析和设计中有一个原则就是:
Favor Composition Over Inheritance。