代理模式

【1】什么是代理模式?

  为其他对象提供一种代理,并以控制对这个对象的访问。

【2】代理模式代码示例:

示例代码:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class SchoolGirl
 6 {
 7 public:
 8     string name;
 9 };
10 
11 /*
12  * 接口
13  */
14 class IGiveGift
15 {
16 public:
17     virtual void giveDolls() = 0;
18     virtual void giveFlowers() = 0;
19 };
20 
21 /*
22  * 委托类
23  */
24 class Pursuit : public IGiveGift
25 {
26 private:
27     SchoolGirl mm;
28 
29 public:
30     Pursuit(SchoolGirl m)
31     {
32         mm = m;
33     }
34     void giveDolls()
35     {
36         cout << mm.name << " 送你娃娃" << endl;    
37     }
38     void giveFlowers()
39     {
40         cout << mm.name << " 送你鲜花" << endl;    
41     }
42 };
43 
44 /*
45  * 代理类
46  */
47 class Proxy : public IGiveGift
48 {
49 private:
50     Pursuit gg;
51 
52 public:
53     Proxy(SchoolGirl mm) : gg(mm)
54     {
55     }
56     void giveDolls()
57     {
58         gg.giveDolls();
59     }
60     void giveFlowers()
61     {
62         gg.giveFlowers();
63     }
64 };
65 
66 /*
67  * 客户端
68  */
69 int main()
70 {
71     SchoolGirl lijiaojiao;
72     lijiaojiao.name = "李娇娇"; 
73     Pursuit zhuojiayi(lijiaojiao); 
74     Proxy daili(lijiaojiao);
75 
76     daili.giveDolls();
77     daili.giveFlowers();
78 
79     return 0;
80 }
View Code

 

Good  Good  Study, Day  Day  Up.

顺序  选择  循环   总结

posted @ 2014-08-28 20:18  kaizenly  阅读(258)  评论(0编辑  收藏  举报
打赏