一、意图:
将抽象部分与实现部分分离,增加灵活性;
二、类图:
三、组成元素:
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(); }