C++模式-Proxy

來源:互聯網
上載者:User

代理模式:為其他對象提供一種代理以控制對這個對象的訪問。

代理模式:⒈遠程代理  也就是為一個對象在不同的地址空間提供局部代表。這樣可以隱藏一個對象存在於不同地址空間的事實(例如WEBSERVICES)

⒉虛擬代理  是根據需要建立開銷很大的對象。通過它來存放執行個體化需要很長時間的真實對象(例如網頁中的圖片,通過虛擬代理來替代真實的圖片,此時代理儲存真實圖片的路徑和尺寸)

⒊安全代理  用來控制真實對象訪問時的許可權

⒋智能指引  指當調用真實對象時,代理處理另外一些事

 

類實現

 

  1. //Proxy.h  
  2. #ifndef AFX_CLASS_SUBJECT  
  3. #define AFX_CLASS_SUBJECT  
  4. class Subject  
  5. {  
  6. public:  
  7.     virtual void Request()=0;  
  8. };  
  9. #endif  
  10.   
  11. #ifndef AFX_CLASS_REALSUBJECT  
  12. #define AFX_CLASS_REALSUBJECT  
  13. class RealSubject:public Subject  
  14. {  
  15. public:  
  16.     virtual void Request()  
  17.     {  
  18.         cout<< "真實的請求" << endl;  
  19.     }  
  20. };  
  21. #endif  
  22.   
  23. #ifndef AFX_CLASS_PROXY  
  24. #define AFX_CLASS_PROXY  
  25. class Proxy:public Subject  
  26. {  
  27. public:  
  28.     Proxy()  
  29.     {  
  30.         realSubject = NULL;  
  31.     }  
  32.     virtual void Request()  
  33.     {  
  34.         if( realSubject == NULL )  
  35.         {  
  36.             realSubject = new RealSubject();  
  37.         }  
  38.         realSubject->Request();  
  39.     }  
  40. private:  
  41.     RealSubject *realSubject;  
  42. };  
  43. #endif  

 

 

主函數實現:

 

  1. #include <iostream>  
  2. #include <string>  
  3. #include <conio.h>  
  4. using namespace std;  
  5. #include "proxy.h"  
  6.   
  7. int main( int argc , char *argv[] )  
  8. {  
  9.     Proxy *proxy = new Proxy();  
  10.     proxy->Request();  
  11.   
  12.     getch();  
  13.     return 1;  
  14. }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.