-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProxy.h
More file actions
74 lines (66 loc) · 1.47 KB
/
Copy pathProxy.h
File metadata and controls
74 lines (66 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
//
// 代理模式 —— 爱情与代理
//
#ifndef DESIGNPATTERN_PROXY_H
#define DESIGNPATTERN_PROXY_H
#include <iostream>
// 被追求者类
class Girl {
private:
std::string _name="";
public:
Girl() = default;
explicit Girl(std::string &name) {
_name = name;
}
std::string Name() {
return _name;
}
};
// 动作接口
class Action {
public:
virtual void GiveChocolate() = 0;
virtual void GiveRoses() = 0;
};
// 追求者类
class Pursuit: public Action {
private:
Girl* _mm = nullptr;
public:
Pursuit() = default;
explicit Pursuit(Girl* mm) {
_mm = mm;
}
void GiveChocolate() override {
std::cout << _mm->Name() << ",给你巧克力!" << std::endl;
}
void GiveRoses() override {
std::cout << _mm->Name() << ",给你玫瑰花!" << std::endl;
}
};
// 代理类
class Proxy: public Action {
private:
Pursuit* _gg = nullptr;
public:
Proxy() = default;
explicit Proxy(Girl* mm) {
_gg = new Pursuit(mm);
}
void GiveChocolate() override {
_gg->GiveChocolate();
}
void GiveRoses() override {
_gg->GiveRoses();
}
};
/*********************************
* 代理模式客户端 *
* string mmName = "Amy"; *
* auto mm = new Girl(mmName); *
* auto bb = new Proxy(mm); *
* bb->GiveChocolate(); *
* bb->GiveRoses(); *
*********************************/
#endif //DESIGNPATTERN_PROXY_H