标签:多个 ret delete ati 接口 部分 style nbsp 模式
1) 意图:
将实现和抽象分离,是它们可以独立的变化
2) 结构:
其中:
a. Abstraction定义对外的接口
b. Implementor定义实现类的接口,是Abstraction的成员指针
c. ConcreteImplementor实现Implementor的具体实现
3) 适用性:
4) 举例:
1 #include <iostream> 2 class Implementor 3 { 4 public: 5 Implementor() {} 6 virtual ~Implementor() {} 7 virtual void OperationImp() = 0; 8 }; 9 class ConcreteImplementorA : public Implementor 10 { 11 public: 12 virtual void OperationImp() 13 { 14 std::cout << "print by ConcreteImplementorA" << std::endl; 15 } 16 }; 17 class ConcreteImplementorB : public Implementor 18 { 19 public: 20 virtual void OperationImp() 21 { 22 std::cout << "print by ConcreteImplementorB" << std::endl; 23 } 24 }; 25 class Abstraction 26 { 27 public: 28 Abstraction(Implementor* imp):m_Imp(imp) {} 29 virtual ~Abstraction() {} 30 void Operation() 31 { 32 if (m_Imp) 33 { 34 m_Imp->OperationImp(); 35 } 36 } 37 private: 38 Implementor* m_Imp; 39 }; 40 41 int main() 42 { 43 Implementor* imp1 = new ConcreteImplementorA(); 44 Abstraction* abs1 = new Abstraction(imp1); 45 abs1->Operation(); 46 47 Implementor* imp2 = new ConcreteImplementorB(); 48 Abstraction* abs2 = new Abstraction(imp2); 49 abs2->Operation(); 50 51 delete imp1; 52 delete abs1; 53 delete imp2; 54 delete abs2; 55 system("pause"); 56 }
标签:多个 ret delete ati 接口 部分 style nbsp 模式
原文地址:https://www.cnblogs.com/ho966/p/12231157.html