1、proxy.h
#ifndef PROXY_H
#define PROXY_H
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Subject //Subject,代理抽象基类,定义了RealSubject和Proxy的公用接口
{
public:
Subject();
virtual ~Subject();
virtual string Get(const string &strKey) = 0;
};
class DataServer : public Subject //RealSubject,是代理的实体类,代理的实际目标
{
public:
DataServer();
virtual ~DataServer();
virtual string Get(const string &strKey);
};
class CacheProxy : public Subject //Proxy,保存一直指向代理实体(RealSubject)的指针代理类
{
public:
CacheProxy();
virtual ~CacheProxy();
virtual string Get(const string &strKey);
protected:
DataServer* mDataServer;
map<string, string> mMapValue; //cache
};
#endif // PROXY_H
2、proxy.cpp
#include "proxy.h"
Subject::Subject(){}
Subject::~Subject(){}
DataServer::DataServer(){}
DataServer::~DataServer(){}
string DataServer::Get(const string &strKey)
{
cout << "Real server query DB, ";
//TODO real server query DB
return strKey;
}
CacheProxy::CacheProxy():mDataServer(new DataServer()){}
CacheProxy::~CacheProxy()
{
delete mDataServer;
}
string CacheProxy::Get(const string &strKey)
{
//target cache
map<string, string>::iterator itr = mMapValue.find(strKey);
if (itr != mMapValue.end())
{
cout << "Target cache, ";
return itr->second;
}
//request from real server
string strVal = mDataServer->Get(strKey);
mMapValue[strKey] = strVal;
return strVal;
}
3、main.cpp
/*
作者:jhluroom弹 QQ:454676244 MSN:jhlu0815@hotmail.com
开发IDE:qt creater
开发环境:QT C++
参考网站:神秘果:http://www.shenmiguo.com/
定义:
代理(Proxy)模式,为其他对象提供一种代理以控制对这个对象的访问。
理解:
1.Subject定义了RealSubject和Proxy的公用接口,使得任何调用RealSubject的地方都可以调用Proxy。
2.RealSubject是代理的实体类。
3.Proxy保存一直指向代理实体(RealSubject)的指针。Proxy与Subject的接口相同,代理就可以替代实体;
Proxy负责RealSubject的创建和管理。
要点:
1.根据代理模式效果的不同,有如下几种代理应用:
远程代理(Remote Proxy)、虚拟代理(Virtual Proxy)、写时拷贝代理(Copy-on-write Proxy)、
保护代理(Protection Proxy)、智能引用代理(Smart Reference Proxy)、缓存代理(Cache Proxy)、
防火墙代理(Firewall Proxy)、同步代理(Synchronization Proxy)。
2. C++语言可通过重载->和*运算符实现类的代理。
Image* ImagePtr::operator-> () {
return LoadImage(); //装载对象并返回对象指针
}
这样对象被引用时,就装载对象,起到了代理的作用。
但这种方式的问题是,只要引用就加载,而不能指定在调用某个方法(如Draw)的时候才加载。
应用:
以缓存代理为例说明,用户向服务器数据库请求数据时,如果所请求的数据在缓存中,则经过代理就可返回要请求的数据;如果不在缓存中,
则真正实际的访问服务器数据库请求数据(这个过程,用户是看不到的,是通过代理来完成的)。
同时,对用户来说,是从真正的服务器中取数据还是从代理中取数据,是不透明的,也就说是用户是不知道的。
以上文字说明,从网上整理而来,有可能部分与其他同仁相同,请谅解,希望我们能够共同交流,谢谢!
*/
#include <QtCore/QCoreApplication>
#include "proxy.h"
int main(int argc, char *argv[])
{
cout << "=== jhluroom start ========" << endl;
//Client,在调用时,并不知道有无DataServerr
string strKey;
string strVal;
Subject* subject = new CacheProxy();
cout << "Request jhlu, ";
strKey= "jhlu";
strVal= subject->Get(strKey);
cout << "key:" << strKey << ", value:" << strVal << endl;
cout << "Request room, ";
strKey = "room";
strVal = subject->Get(strKey);
cout << "key:" << strKey << ", value:" << strVal << endl;
cout << "Request jhlu, ";
strKey = "jhlu";
strVal = subject->Get(strKey);
cout << "key:" << strKey << ", value:" << strVal << endl;
cout << "Request room, ";
strKey = "room";
strVal = subject->Get(strKey);
cout << "key:" << strKey << ", value:" << strVal << endl;
cout << "=== jhluroom finish _^_ ===" << endl;
return 0;
}
运行结果:
=== jhluroom start ========
Request jhlu, Real server query DB, key:jhlu, value:jhluRequest room, Real server query DB, key:room, value:room
Request jhlu, Target cache, key:jhlu, value:jhlu
Request room, Target cache, key:room, value:room
=== jhluroom finish _^_ ===