-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecorator.h
More file actions
70 lines (63 loc) · 1.42 KB
/
Copy pathDecorator.h
File metadata and controls
70 lines (63 loc) · 1.42 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
//
// 装饰模式 —— 服装搭配
// 把需要的功能按正确顺序串联起来进行控制
//
#ifndef DESIGNPATTERN_DECORATOR_H
#define DESIGNPATTERN_DECORATOR_H
#include <iostream>
#include <string>
// ConcreteComponent
class Person {
private:
std::string _name;
public:
Person() {
_name = "";
}
explicit Person(std::string &name) {
_name = name;
}
virtual void Show() {
std::cout << "装扮的" << _name << std::endl;
}
};
// Decorator
class Finery: public Person {
protected:
Person* _component;
public:
void Decorate(Person* component) {
_component = component;
}
void Show() override {
if(_component != nullptr) {
_component->Show();
}
}
};
// ConcreteDecorator
class TShirt: public Finery {
public:
void Show() override {
std::cout << "大T恤 ";
Finery::Show();
}
};
class BigTrouser: public Finery {
public:
void Show() override {
std::cout << "阔腿裤 ";
Finery::Show();
}
};
/*********************************
* 装饰模式客户端 *
* string name = "小柯"; *
* auto k = new Person(name); *
* auto ts = new TShirt(); *
* auto bt = new BigTrouser(); *
* bt->Decorate(k); *
* ts->Decorate(bt); *
* ts->Show(); *
*********************************/
#endif //DESIGNPATTERN_DECORATOR_H