一、意图:
将抽象部分与实现部分分离,增加灵活性;
二、类图:
三、组成元素:
Abstraction:抽象类接口;
RefinedAbstraction:扩展Abstraction的接口;
Implementor:实现类接口;
ConcreteImplementor:具体实现类;
四、代码实现;
#include "iostream" using namespace std; class Implementor { public: virtual void Operation()=0; }; class ConcreteImplementorA:public Implementor { public: void Operation() { cout<<"ConcreteImplementorA!"<<endl; } }; class Abstraction { private: Implementor* p_Imp; public: Abstraction(Implementor* imp) { p_Imp=imp; } void Operation() { p_Imp->Operation(); } }; class RefinedAbstraction:public Abstraction { public: void Operation() { } }; class ConcreteImplementorB:public Implementor { public: void Operation() { cout<<"ConcreteImplementorB!"<<endl; } }; void main() { Implementor* ima=new ConcreteImplementorA(); Implementor* imb=new ConcreteImplementorB(); Abstraction* abs1=new Abstraction(ima); Abstraction* abs2=new Abstraction(imb); abs1->Operation(); abs2->Operation(); }
本文介绍了一种设计模式——桥接模式,旨在通过将抽象部分与其实现部分分离来提高系统的灵活性。文中详细展示了该模式的类图及组成部分,并提供了一个使用C++实现的具体示例。

1324

被折叠的 条评论
为什么被折叠?



