码迷,mamicode.com
首页 > 其他好文 > 详细

Proxy Pattern

时间:2015-07-20 21:16:38      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

1.Proxy Pattern最大的好处就是实现了逻辑和实现的彻底解耦。

2.Proxy Pattern结构图

技术分享

3.实现

技术分享
 1 #ifndef _PROXY_H_ 
 2 #define _PROXY_H_
 3 
 4 class Subject 
 5 { 
 6 public: 
 7     virtual ~Subject();
 8     virtual void Request() = 0;
 9 protected: 
10     Subject();
11 private:
12 };
13 
14 class ConcreteSubject:public Subject 
15 { 
16 public:
17     ConcreteSubject();
18     ~ConcreteSubject();
19     void Request();
20 protected:
21 private:
22 };
23 
24 class Proxy:public Subject
25 { 
26 public: 
27     Proxy();
28     Proxy(Subject* sub);
29     ~Proxy();
30     void Request();
31 protected: 
32 private: 
33     Subject* _sub; 
34 };
35 
36 #endif
Proxy.h
技术分享
 1 #include "Proxy.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 Subject::Subject() 
 6 {
 7 
 8 }
 9 Subject::~Subject() 
10 {
11 
12 }
13 ConcreteSubject::ConcreteSubject() 
14 {
15 
16 }
17 ConcreteSubject::~ConcreteSubject() 
18 {
19 
20 }
21 void ConcreteSubject::Request() 
22 { 
23     cout<<"ConcreteSubject......request ...."<<endl;
24 }
25 Proxy::Proxy() 
26 {
27 
28 }
29 Proxy::Proxy(Subject* sub) 
30 { 
31     _sub = sub;
32 }
33 Proxy::~Proxy() 
34 {
35     delete _sub; 
36 }
37 void Proxy::Request() 
38 { 
39     cout<<"Proxy request...."<<endl;
40     _sub->Request(); 
41 }
Proxy.cpp
技术分享
 1 #include "Proxy.h"
 2 #include <iostream> 
 3 using namespace std;
 4 
 5 int main(int argc,char* argv[]) 
 6 { 
 7     Subject* sub = new ConcreteSubject();
 8     Proxy* p = new Proxy(sub);
 9     p->Request();
10     return 0; 
11 }
main.cpp

Proxy Pattern

标签:

原文地址:http://www.cnblogs.com/programmer-wfq/p/4662224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!