设计模式--装饰器模式
1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 public: 7 virtual void doTask(){ 8 cout<<"A Task"<<endl; 9 }; 10 }; 11 12 class B : public A 13 { 14 public: 15 B(A* a) 16 { 17 _m = a; 18 } 19 20 void doTask() 21 { 22 _m->doTask(); 23 cout<<"then B Task"<<endl; 24 } 25 26 private: 27 A* _m; 28 }; 29 30 class C : public A 31 { 32 public: 33 C(A* a) 34 { 35 _m = a; 36 } 37 38 void doTask() 39 { 40 _m->doTask(); 41 cout<<"then C Task"<<endl; 42 } 43 44 private: 45 A* _m; 46 }; 47 48 int main() 49 { 50 C c(new B(new A())); 51 c.doTask(); 52 return 0; 53 }