目录
将抽象部分与它的实现部分分离,使它们都可以独立地变化。

定义抽象类的接口。
维护一个指向Implementor类型对象的指针。
扩充由Abstraction定义的接口。
定义实现类的接口,该接口不一定要与Abstraction的接口完全一致;事实上这两个接口可以完全不同。一般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基本这些基本操作的较高层次的操作。
ConcreteImplementor
实现了Implementor接口并定义它的具体实现。
- #include
- using namespace std;
-
- class Implementor {
- public:
- virtual void OperationImp() = 0;
- };
-
- class ConcreteImplementorA : public Implementor {
- public:
- virtual void OperationImp() {
- cout << "Concrete Implementor A" << endl;
- }
- };
-
- class ConcreteImplementorB : public Implementor {
- public:
- virtual void OperationImp() {
- cout << "Concrete Implementor B" << endl;
- }
- };
-
- class Abstraction {
- public:
- virtual void Operation() = 0;
- };
-
- class RefinedAbstraction : public Abstraction {
- public:
- RefinedAbstraction(Implementor* TempImplementor) {
- this->implementor = TempImplementor;
- }
- void Operation() {
- implementor->OperationImp();
- }
- private:
- Implementor* implementor;
- };
-
- int main() {
-
- Implementor* implementorA = new ConcreteImplementorA;
- Abstraction* abstractionA = new RefinedAbstraction(implementorA);
- abstractionA->Operation();
-
- Implementor* implementorB = new ConcreteImplementorB;
- Abstraction* abstractionB = new RefinedAbstraction(implementorB);
- abstractionB->Operation();
-
- return 0;
- }