1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <queue>
5 #include <set>
6 #include <algorithm>
7 #include <map>
8 #include <stack>
9 using namespace std;
10
11 // 女学生类
12 // 相当于client类
13 class cSchoolGirl {
14 private:
15 string m_name; // 姓名
16
17 public:
18 string GetName() const {
19 return m_name;
20 }
21 void SetName(string name) {
22 m_name = name;
23 }
24 };
25
26 // 送礼物接口
27 // 相当于Subject类,定义了实体和代理的公共接口
28 class IGiveGift {
29 public:
30 virtual string GiveFlowers() = 0;
31 };
32
33 // 追求者
34 // 相当于RealSubject类,实际工作的类
35 class cPursuit: public IGiveGift {
36 private:
37 cSchoolGirl* m_mm;
38
39 public:
40 cPursuit(cSchoolGirl* mm) :
41 m_mm(mm) {
42 }
43
44 string GiveFlowers() {
45 return m_mm->GetName() + string(" 送你鲜花");
46 }
47 };
48
49 // 代理类
50 // 即Proxy类,主要的功能是代理RealSubject接受外界的调用
51 // 使得减少了RealSubject与外界的耦合
52 class cProxy: public IGiveGift {
53 private:
54 cPursuit* m_gg;
55
56 public:
57 cProxy(cSchoolGirl* mm) {
58 m_gg = new cPursuit(mm);
59 }
60 // 代理的时候可以添加一点额外的功能,例如多加一句“你好”
61 string GiveFlowers() {
62 return string("你好,") + m_gg->GiveFlowers();
63 }
64 };
65
66 int main() {
67 cSchoolGirl jiaojiao;
68 jiaojiao.SetName("caroline");
69 cProxy daili(&jiaojiao);
70 cout << daili.GiveFlowers() << endl;
71 return 0;
72 }