proxy patter --代理模式

来源:互联网 发布:ai人工智能培训班 编辑:程序博客网 时间:2024/06/04 19:35

分类: 设计模式 229人阅读 评论(2) 收藏 举报
设计模式
wrapper:对接口编程,在保持接口不变的情况下,加入一些附加的功能比如  隐藏远程调用,

对于复杂的软件系统常常有一种处理手法,即增加一层间接层,从而使得系统获得一种更为灵活、满足特定需求的解决方案。在面向对象的系统中,有些对象由于某种原因,比如对象创建的开销很大,或者某些操作需要安全控制,或者需要进程外的访问等,直接访问会给使用者或者系统结构带来很多麻烦。
Proxy设计模式就是在不失去透明操作对象的同时,通过增加一层间接层来管理、控制这些对象特有的复杂性。

[cpp] view plaincopy
  1. // Proxy.h  
  2. #include <string>  
  3. #include <iostream>  
  4. #include <string>  
  5.    
  6. class IEmployee  
  7. {  
  8. public:  
  9.          virtual string get_name(int ID) = 0;  
  10.          virtual int get_age(int ID) = 0;  
  11.          virtual double get_salary(int ID) = 0;  
  12.    
  13. public:  
  14.          virtual ~IEmployee();  
  15. };  
  16.    
  17. IEmployee::~IEmployee()  
  18. {  
  19.          cout << "in the destructor of IEmployee..." << endl;  
  20. }  
  21.    
  22. class Employee : public IEmployee  
  23. {  
  24. public:  
  25.          string get_name(int ID);  
  26.          int get_age(int ID);  
  27.          double get_salary(int ID);  
  28.          ~Employee();  
  29. };  
  30.    
  31. string Employee::get_name(int ID)  
  32. {  
  33.          // ... 假定此处查询数据库,获得ID对应员工的姓名  
  34.          string name = "tom";  
  35.          return name;  
  36. }  
  37.    
  38. int Employee::get_age(int ID)  
  39. {  
  40.          // ... 假定此处查询数据库,获得ID对应员工的年龄  
  41.          int age = 20;  
  42.          return age;  
  43. }  
  44.    
  45. double Employee::get_salary(int ID)  
  46. {  
  47.          // ... 假定此处查询数据库,获得ID对应员工的工资  
  48.          double salary = 100000.00;  
  49.          return salary;  
  50. }  
  51.    
  52. Employee::~Employee()  
  53. {  
  54.          cout << "in the destructor of Employee..." << endl;  
  55. }  
  56.    
  57. //代理  
  58. class EmployeeProxy : public IEmployee  
  59. {  
  60. public:  
  61.          string get_name(int ID);  
  62.          int get_age(int ID);  
  63.          double get_salary(int ID);  
  64.          ~EmployeeProxy();  
  65. };  
  66.    
  67. string EmployeeProxy::get_name(int ID)  
  68. {  
  69.          // ...假定此处通过socket或者RPC等其他方式访问Employee中的get_name(int ID)方法,并接受相应的返回值  
  70.          string name = "tom";  
  71.          return name;  
  72. }  
  73.    
  74. int EmployeeProxy::get_age(int ID)  
  75. {  
  76.          // ...假定此处通过socket或者RPC等其他方式访问Employee中的get_age(int ID)方法,并接受相应的返回值  
  77.          int age = 20;  
  78.          return age;  
  79. }  
  80.    
  81. double EmployeeProxy::get_salary(int ID)  
  82. {  
  83.          // ...假定此处通过socket或者RPC等其他方式访问Employee中的get_salary(int ID)方法,并接受相应的返回值  
  84.          double salary = 100000.00;  
  85.          return salary;  
  86. }  
  87.    
  88. EmployeeProxy::~EmployeeProxy()  
  89. {  
  90.          cout << "in the destructor of EmployeeProxy..." << endl;  
  91. }  
  92.    
  93. // Proxy.cpp  
  94. #include "Proxy.h"  
  95.    
  96. int main(int argc, char **argv)  
  97. {  
  98.          IEmployee *employee = new EmployeeProxy;  
  99.          cout << employee->get_name(10) << endl;  
  100.          cout << employee->get_age(10) << endl;  
  101.          cout << employee->get_salary(10) << endl;  
  102.          delete employee;  
  103.    
  104.          return 0;  
  105. }