003 --- 第6章 装饰模式

简述:

  装饰模式动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

  装饰模式包括:抽象对象基类、具体对象类、抽象装饰类、具体装饰类。

    抽象对象基类:定义一个对象接口,可以给这些对象动态的添加职责。

    具体对象类:继承自抽象对象基类,定义了具体的对象,可以给这个对象添加一些职责。

    抽象装饰类:继承自抽象对象基类,从类外扩展抽象对象基类的功能,但对于抽象对象基类来说,是无需知道抽象装饰类的存在的。

    具体装饰类:就是具体的装饰对象类、起到给抽象对象基类添加职责的功能。

装饰模式代码:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 装饰模式
 5 // 抽象对象基类
 6 class CComponent
 7 {
 8 public:
 9     virtual void Operation() = 0;
10 };
11 
12 // 具体对象类
13 class CConcreteComponent : public CComponent
14 {
15 public:
16     virtual void Operation()
17     {
18         cout << "具体对象的操作" << endl;
19     }
20 };
21 
22 // 抽象装饰类
23 class CDecorator : public CComponent
24 {
25 protected:
26     CComponent* m_pComponent;
27 
28 public:
29     CDecorator() : m_pComponent(NULL) {};
30 
31     void SetComponent(CComponent* pComponent)
32     {
33         m_pComponent = pComponent;
34     }
35 
36     virtual void Operation()
37     {
38         if (m_pComponent != NULL)
39         {
40             m_pComponent->Operation();
41         }
42     }
43 };
44 
45 // 具体装饰类
46 class CConcreteDecDecoratorA : public CDecorator
47 {
48 private:
49     string m_AddedState;
50 
51 public:
52     virtual void Operation()
53     {
54         CDecorator::Operation();
55         m_AddedState = "New State";
56         cout << "具体装饰对象A的操作" << endl;
57     }
58 };
59 
60 // 具体装饰类
61 class CConcreteDecDecoratorB : public CDecorator
62 {
63 public:
64     virtual void Operation()
65     {
66         CDecorator::Operation();
67         AddedBehavior();
68         cout << "具体装饰对象B的操作" << endl;
69     }
70 
71 private:
72     void AddedBehavior()
73     {
74         cout << "具体装饰对象B新增的行为" << endl;
75     }
76 };
77 
78 int main()
79 {
80     CConcreteComponent ConcreteComponent;
81     CConcreteDecDecoratorA ConcreteDecDecoratorA;
82     CConcreteDecDecoratorB ConcreteDecDecoratorB;
83 
84     ConcreteDecDecoratorA.SetComponent(&ConcreteComponent);
85     ConcreteDecDecoratorB.SetComponent(&ConcreteDecDecoratorA);
86     ConcreteDecDecoratorB.Operation();
87 
88     system("pause");
89     return 0;
90 }

输出结果:

例:GHL见妹子的打扮

代码如下:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 // 人的类(具体对象类,本例不需要抽象对象基类)
  5 class CPerson
  6 {
  7 private:
  8     string m_szName;
  9 
 10 public:
 11     CPerson(){}
 12 
 13     CPerson(string szName)
 14     {
 15         m_szName = szName;
 16     }
 17 
 18     virtual void Show()
 19     {
 20         string str = "装扮的[" + m_szName + "]";
 21         cout << str << endl;
 22     }
 23 };
 24 
 25 // 服装抽象类(抽象装饰类,继承自具体对象类)
 26 class CFinery : public CPerson
 27 {
 28 protected:
 29     CPerson* m_pComponent;
 30 
 31 public:
 32     void Decorate(CPerson* pComponent)
 33     {
 34         m_pComponent = pComponent;
 35     }
 36     virtual void Show()
 37     {
 38         if (m_pComponent)
 39             m_pComponent->Show();
 40     }
 41 };
 42 
 43 // 具体服饰类
 44 // 大T恤类(具体装饰类,继承自抽象装饰类)
 45 class CTShirts : public CFinery
 46 {
 47 public:
 48     virtual void Show()
 49     {
 50         cout << "大T恤 ";
 51         CFinery::Show();
 52     }
 53 };
 54 
 55 // 垮裤类(具体装饰类,继承自抽象装饰类)
 56 class CBigTrouser : public CFinery
 57 {
 58 public:
 59     virtual void Show()
 60     {
 61         cout << "垮裤 ";
 62         CFinery::Show();
 63     }
 64 };
 65 
 66 // 破球鞋类(具体装饰类,继承自抽象装饰类)
 67 class CSneakers : public CFinery
 68 {
 69 public:
 70     virtual void Show()
 71     {
 72         cout << "破球鞋 ";
 73         CFinery::Show();
 74     }
 75 };
 76 
 77 // 西装类(具体装饰类,继承自抽象装饰类)
 78 class CSuit : public CFinery
 79 {
 80 public:
 81     virtual void Show()
 82     {
 83         cout << "西装 ";
 84         CFinery::Show();
 85     }
 86 };
 87 
 88 // 领带类(具体装饰类,继承自抽象装饰类)
 89 class CTie : public CFinery
 90 {
 91 public:
 92     virtual void Show()
 93     {
 94         cout << "领带 ";
 95         CFinery::Show();
 96     }
 97 };
 98 
 99 // 皮鞋类(具体装饰类,继承自抽象装饰类)
100 class CLeatherShoes : public CFinery
101 {
102 public:
103     virtual void Show()
104     {
105         cout << "皮鞋 ";
106         CFinery::Show();
107     }
108 };
109 
110 int main()
111 {
112     CPerson Person("GHL");
113 
114     cout << "第一种装扮:" << endl;
115     CSneakers Sneakers;
116     CBigTrouser BigTrouser;
117     CTShirts TShirts;
118     Sneakers.Decorate(&Person);
119     BigTrouser.Decorate(&Sneakers);
120     TShirts.Decorate(&BigTrouser);
121     TShirts.Show();
122 
123     cout << "第二种装扮:" << endl;
124     CLeatherShoes LeatherShoes;
125     CTie Tie;
126     CSuit Suit;
127     LeatherShoes.Decorate(&Person);
128     Tie.Decorate(&LeatherShoes);
129     Suit.Decorate(&Tie);
130     Suit.Show();
131 
132     system("pause");
133     return 0;
134 }

输出结果:

原文地址:https://www.cnblogs.com/SmallAndGreat/p/13470442.html