Decorator designpattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <string>
#include <iostream>
using namespace std;
//人
class Person
{
private:
string m_strName;
public:
Person(string strName)
{
m_strName=strName;
}
Person(){}
virtual void Show()
{
cout<<"装扮的是:"<<m_strName<<endl;
}
};
//装饰类
class Finery :public Person
{
protected:
Person* m_component;
public:
void Decorate(Person* component)
{
m_component=component;
}
virtual void Show()
{
m_component->Show();
}
};
//T恤
class TShirts: public Finery
{
public:
virtual void Show()
{
cout<<"T Shirts"<<endl;
m_component->Show();
}
};
//裤子
class BigTrouser :public Finery
{
public:
virtual void Show()
{
cout<<" Big Trouser"<<endl;
m_component->Show();
}
};
//客户端
int main()
{
Person *p=new Person("小李");
BigTrouser *bt=new BigTrouser();
TShirts *ts=new TShirts();
bt->Decorate(p);
ts->Decorate(bt);
ts->Show();
return 0;
}