C++设计模式之-代理模式

根据程洁的大话模式:

 1 // Proxy.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<iostream>
 6 #include <string>
 7 using namespace std;
 8 
 9 class Girl
10 {
11 public:
12     Girl(string name = ""):mName(name){}
13     string getName()
14     {
15         return mName;
16     }
17 private:
18     string mName;
19 };
20 
21 class GiveGift
22 {
23 public:
24     virtual void GiveDolls() = 0;
25     virtual void GiveFlowers() = 0;
26     virtual void GiveChocolate() = 0;
27 };
28 
29 class Puisuit:public GiveGift
30 {
31 public:
32     Puisuit(Girl mm):mGirl(mm){}
33     virtual void GiveDolls()
34     {
35         cout<<""<<mGirl.getName()<<"玩具"<<endl;
36     }
37     virtual void GiveFlowers()
38     {
39         cout<<""<<mGirl.getName()<<"鲜花"<<endl;
40     }
41     virtual void GiveChocolate()
42     {
43         cout<<""<<mGirl.getName()<<"巧克力"<<endl;
44     }
45 private:
46     Girl mGirl;
47 };
48 
49 class Proxy:public GiveGift
50 {
51 public:
52     Proxy(Girl mm)
53     {
54         mPuisuit = new Puisuit(mm);
55     }
56     virtual void GiveDolls()
57     {
58         mPuisuit->GiveDolls();
59     }
60     virtual void GiveFlowers()
61     {
62         mPuisuit->GiveFlowers();
63     }
64     virtual void GiveChocolate()
65     {
66         mPuisuit->GiveChocolate();
67     }
68 private:
69     Puisuit* mPuisuit;
70 };
71 int _tmain(int argc, _TCHAR* argv[])
72 {
73     cout<<"代理模式"<<endl;
74     Girl girl("娇娇");
75     Proxy pro(girl);
76     pro.GiveDolls();
77     pro.GiveFlowers();
78     pro.GiveChocolate();
79     system("pause");
80     return 0;
81 }

 

posted @ 2018-07-06 12:01  wxmwanggood  阅读(172)  评论(0编辑  收藏  举报