Repository navigation
Expand file tree
/
Copy pathDecoratorPattern.h
More file actions
94 lines (88 loc) · 1.47 KB
/
Copy pathDecoratorPattern.h
File metadata and controls
94 lines (88 loc) · 1.47 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef __DECORATOR_PATTERN_H__
#define __DECORATOR_PATTERN_H__
//抽象构件
class Component
{
public:
Component(){}
virtual ~Component(){}
virtual void operation() = 0;
};
//具体构件类
class Phone :public Component
{
public:
Phone(){}
void operation(){
printf("手机\n");
}
};
//抽象装饰类
class Decorator :public Component
{
public:
Decorator(){}
Decorator(Component *c){
this->component = c;
}
void operation(){
this->component->operation();
}
Component *getComponent(){
return this->component;
}
void setComponent(Component *c){
this->component = c;
}
private:
Component *component;
};
//具体装饰类:手机壳
class DecoratorShell:public Decorator
{
public:
DecoratorShell(){}
DecoratorShell(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("装手机壳\n");
}
};
//具体装饰类:手机贴纸
class DecoratorSticker :public Decorator
{
public:
DecoratorSticker(){}
DecoratorSticker(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("贴卡通贴纸\n");
}
};
//具体装饰类:手机挂绳
class DecoratorRope :public Decorator
{
public:
DecoratorRope(){}
DecoratorRope(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("系手机挂绳\n");
}
};
#endif //__DECORATOR_PATTERN_H__