简述:
装饰模式动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
装饰模式包括:抽象对象基类、具体对象类、抽象装饰类、具体装饰类。
抽象对象基类:定义一个对象接口,可以给这些对象动态的添加职责。
具体对象类:继承自抽象对象基类,定义了具体的对象,可以给这个对象添加一些职责。
抽象装饰类:继承自抽象对象基类,从类外扩展抽象对象基类的功能,但对于抽象对象基类来说,是无需知道抽象装饰类的存在的。
具体装饰类:就是具体的装饰对象类、起到给抽象对象基类添加职责的功能。
装饰模式代码:
1 // 抽象对象基类 2 class CComponent 3 { 4 public: 5 virtual void Operation() = 0; 6 }; 7 8 // 具体对象类 9 class CConcreteComponent : public CComponent 10 { 11 public: 12 virtual void Operation() 13 { 14 cout << "具体对象的操作" << endl; 15 } 16 }; 17 18 // 抽象装饰类 19 class CDecorator : public CComponent 20 { 21 protected: 22 CComponent* m_pComponent; 23 24 public: 25 CDecorator() : m_pComponent(NULL) {}; 26 27 void SetComponent(CComponent* pComponent) 28 { 29 m_pComponent = pComponent; 30 } 31 32 virtual void Operation() 33 { 34 if (m_pComponent != NULL) 35 { 36 m_pComponent->Operation(); 37 } 38 } 39 }; 40 41 // 具体装饰类 42 class CConcreteDecDecoratorA : public CDecorator 43 { 44 private: 45 string m_AddedState; 46 47 public: 48 virtual void Operation() 49 { 50 CDecorator::Operation(); 51 m_AddedState = "New State"; 52 cout << "具体装饰对象A的操作" << endl; 53 } 54 }; 55 56 // 具体装饰类 57 class CConcreteDecDecoratorB : public CDecorator 58 { 59 public: 60 virtual void Operation() 61 { 62 CDecorator::Operation(); 63 AddedBehavior(); 64 cout << "具体装饰对象B的操作" << endl; 65 } 66 67 private: 68 void AddedBehavior() 69 { 70 cout << "具体装饰对象B新增的行为" << endl; 71 } 72 }; 73 74 int main() 75 { 76 CConcreteComponent ConcreteComponent; 77 CConcreteDecDecoratorA ConcreteDecDecoratorA; 78 CConcreteDecDecoratorB ConcreteDecDecoratorB; 79 80 ConcreteDecDecoratorA.SetComponent(&ConcreteComponent); 81 ConcreteDecDecoratorB.SetComponent(&ConcreteDecDecoratorA); 82 ConcreteDecDecoratorB.Operation(); 83 84 system("pause"); 85 return 0; 86 }
输出结果:
例:GHL见妹子的打扮
代码如下:
1 // 人的类(具体对象类,本例不需要抽象对象基类) 2 class CPerson 3 { 4 private: 5 string m_szName; 6 7 public: 8 CPerson(){} 9 10 CPerson(string szName) 11 { 12 m_szName = szName; 13 } 14 15 virtual void Show() 16 { 17 string str = "装扮的[" + m_szName + "]"; 18 cout << str << endl; 19 } 20 }; 21 22 // 服装抽象类(抽象装饰类,继承自具体对象类) 23 class CFinery : public CPerson 24 { 25 protected: 26 CPerson* m_pComponent; 27 28 public: 29 void Decorate(CPerson* pComponent) 30 { 31 m_pComponent = pComponent; 32 } 33 virtual void Show() 34 { 35 if (m_pComponent) 36 m_pComponent->Show(); 37 } 38 }; 39 40 // 具体服饰类 41 // 大T恤类(具体装饰类,继承自抽象装饰类) 42 class CTShirts : public CFinery 43 { 44 public: 45 virtual void Show() 46 { 47 cout << "大T恤 "; 48 CFinery::Show(); 49 } 50 }; 51 52 // 垮裤类(具体装饰类,继承自抽象装饰类) 53 class CBigTrouser : public CFinery 54 { 55 public: 56 virtual void Show() 57 { 58 cout << "垮裤 "; 59 CFinery::Show(); 60 } 61 }; 62 63 // 破球鞋类(具体装饰类,继承自抽象装饰类) 64 class CSneakers : public CFinery 65 { 66 public: 67 virtual void Show() 68 { 69 cout << "破球鞋 "; 70 CFinery::Show(); 71 } 72 }; 73 74 // 西装类(具体装饰类,继承自抽象装饰类) 75 class CSuit : public CFinery 76 { 77 public: 78 virtual void Show() 79 { 80 cout << "西装 "; 81 CFinery::Show(); 82 } 83 }; 84 85 // 领带类(具体装饰类,继承自抽象装饰类) 86 class CTie : public CFinery 87 { 88 public: 89 virtual void Show() 90 { 91 cout << "领带 "; 92 CFinery::Show(); 93 } 94 }; 95 96 // 皮鞋类(具体装饰类,继承自抽象装饰类) 97 class CLeatherShoes : public CFinery 98 { 99 public: 100 virtual void Show() 101 { 102 cout << "皮鞋 "; 103 CFinery::Show(); 104 } 105 }; 106 107 int main() 108 { 109 CPerson Person("GHL"); 110 111 cout << "第一种装扮:" << endl; 112 CSneakers Sneakers; 113 CBigTrouser BigTrouser; 114 CTShirts TShirts; 115 Sneakers.Decorate(&Person); 116 BigTrouser.Decorate(&Sneakers); 117 TShirts.Decorate(&BigTrouser); 118 TShirts.Show(); 119 120 cout << "第二种装扮:" << endl; 121 CLeatherShoes LeatherShoes; 122 CTie Tie; 123 CSuit Suit; 124 LeatherShoes.Decorate(&Person); 125 Tie.Decorate(&LeatherShoes); 126 Suit.Decorate(&Tie); 127 Suit.Show(); 128 129 system("pause"); 130 return 0; 131 }
输出结果:
原文:https://www.cnblogs.com/SmallAndGreat/p/13470442.html