一、相关介绍
1、代理模式为其他对象提供一种代理以控制对这个对象的访问
2、UML图
3、所属类别:结构型
3、C++程序
1 // 代理模式.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include<iostream> 6 using namespace std; 7 //定义一个代理和商人之间的接口 8 class interface_businessman_proxy 9 { 10 public: 11 interface_businessman_proxy(){} 12 ~interface_businessman_proxy(){} 13 virtual void apple()=0; 14 virtual void huawei()=0; 15 virtual void zhongxing()=0; 16 virtual void xiaomi()=0; 17 }; 18 //商人类,被代理的类 19 class Businessman:public interface_businessman_proxy 20 { 21 public: 22 Businessman(){} 23 ~Businessman(){} 24 virtual void apple() 25 { 26 cout<<"卖aphone"<<endl; 27 } 28 virtual void huawei() 29 { 30 cout<<"卖华为手机"<<endl; 31 } 32 virtual void zhongxing() 33 { 34 cout<<"买中兴手机"<<endl; 35 } 36 virtual void xiaomi() 37 { 38 cout<<"卖小米手机"<<endl; 39 } 40 }; 41 //代理类 42 class Proxy:public interface_businessman_proxy 43 { 44 private: 45 interface_businessman_proxy *businessman; 46 public: 47 Proxy() 48 { 49 businessman=new Businessman(); 50 } 51 ~Proxy(){} 52 virtual void apple() 53 { 54 (*businessman).apple(); 55 } 56 virtual void huawei() 57 { 58 (*businessman).huawei(); 59 } 60 virtual void zhongxing() 61 { 62 (*businessman).zhongxing(); 63 } 64 virtual void xiaomi() 65 { 66 (*businessman).xiaomi(); 67 } 68 }; 69 int _tmain(int argc, _TCHAR* argv[]) 70 { 71 Proxy *daili=new Proxy(); 72 (*daili).huawei(); 73 (*daili).xiaomi(); 74 return 0; 75 }