轉(zhuǎn)載自:
http://patmusing.blog.163.com/blog/static/13583496020101410329439/
Proxy also a.k.a. Surrogate
對(duì)于復(fù)雜的軟件系統(tǒng)常常有一種處理手法,即增加一層間接層,從而使得系統(tǒng)獲得一種更為靈活、滿足特定需求的解決方案。在面向?qū)ο蟮南到y(tǒng)中,有些對(duì)象由于某種原因,比如對(duì)象創(chuàng)建的開(kāi)銷很大,或者某些操作需要安全控制,或者需要進(jìn)程外的訪問(wèn)等,直接訪問(wèn)會(huì)給使用者或者系統(tǒng)結(jié)構(gòu)帶來(lái)很多麻煩。
Proxy設(shè)計(jì)模式就是在不失去透明操作對(duì)象的同時(shí),通過(guò)增加一層間接層來(lái)管理、控制這些對(duì)象特有的復(fù)雜性。

圖1
圖2
表示不同的進(jìn)程
圖1中假定A和B位于不同的進(jìn)程,典型的情況是A和B位于互聯(lián)網(wǎng)上兩臺(tái)不同的機(jī)器上,如果A要訪問(wèn)B三次,那么A就要如上圖調(diào)用B三次。
圖2中增加一個(gè)B的代理,該代理和A位于同一個(gè)進(jìn)程內(nèi),當(dāng)A要調(diào)用B的功能時(shí),A僅需要調(diào)用B的代理即可,然后B的代理和B之間進(jìn)行通信。另外如果A要3次調(diào)用B的功能,那么A僅需要3此調(diào)用B的代理即可,而B的代理和B之間有可能只需要一次通信就夠了,由此可見(jiàn),B的代理有一定的緩沖功能。
Provide a surrogate or placeholder for another object to control access to it. (為其他對(duì)象提供一種代理以控制對(duì)這個(gè)對(duì)象的訪問(wèn)) -GoF
Proxy設(shè)計(jì)模式之靜態(tài)結(jié)構(gòu)UML類圖:

圖3
說(shuō)明:
Service:
The interface that both the proxy and the real object will implement. (代理和實(shí)際對(duì)象必須實(shí)現(xiàn)的接口)
ServiceProxy:
ServiceProxy implements Service and forwards method calls to the real object (ServiceImplemention) when appropriate.
(ServiceProxy實(shí)現(xiàn)了Service接口,并在合適的時(shí)候,將請(qǐng)求傳遞給實(shí)際對(duì)象ServiceImplementation)
ServiceImplementation:
The real, full implementation of the interface. This object will be represented by the Proxy object.
(真正、完整的接口實(shí)現(xiàn),該對(duì)象由Proxy對(duì)象代表)
A proxy (or stub) is a representative for another object. To enable the proxy to represent the real object, the proxy has to implement the exact same interface as the real object. Furthermore, the proxy keeps a reference to the real object. The proxy needs the reference to the real object so that it can call methods on the real object if necessary. The clients will be interacting with the proxy, but the proxy can delegate the execution to the real object. The proxy implements the same interface as the real object, but can perform tasks that the real object does not, such as remote communication or security.
Use the Proxy pattern when you need a more elaborate reference to an object instead of just a regular one:
- Remote proxy: When you need a local representative for an object in another address space(e.g. JVM)
- Virtual proxy: Acts as a placeholder and delays creating expensive objects.
- Protection proxy: Determines access rights to the real object.
Remote Proxy C++模擬代碼:
// Proxy.h
#include <string>
#include <iostream>
#include <string>
using namespace std;
class IEmployee
{
public:
virtual string get_name(int ID) = 0;
virtual int get_age(int ID) = 0;
virtual double get_salary(int ID) = 0;
public:
virtual ~IEmployee();
};
IEmployee::~IEmployee()
{
cout << "in the destructor of IEmployee..." << endl;
}
class Employee : public IEmployee
{
public:
string get_name(int ID);
int get_age(int ID);
double get_salary(int ID);
~Employee();
};
string Employee::get_name(int ID)
{
// ... 假定此處查詢數(shù)據(jù)庫(kù),獲得ID對(duì)應(yīng)員工的姓名
string name = "玄機(jī)逸士";
return name;
}
int Employee::get_age(int ID)
{
// ... 假定此處查詢數(shù)據(jù)庫(kù),獲得ID對(duì)應(yīng)員工的年齡
int age = 27;
return age;
}
double Employee::get_salary(int ID)
{
// ... 假定此處查詢數(shù)據(jù)庫(kù),獲得ID對(duì)應(yīng)員工的工資
double salary = 50000.1;
return salary;
}
Employee::~Employee()
{
cout << "in the destructor of Employee..." << endl;
}
class EmployeeProxy : public IEmployee
{
public:
string get_name(int ID);
int get_age(int ID);
double get_salary(int ID);
~EmployeeProxy();
};
string EmployeeProxy::get_name(int ID)
{
// ...假定此處通過(guò)socket或者RPC等其他方式訪問(wèn)Employee中的get_name(int ID)方法,并接受相應(yīng)的返回值
string name = "玄機(jī)逸士";
return name;
}
int EmployeeProxy::get_age(int ID)
{
// ...假定此處通過(guò)socket或者RPC等其他方式訪問(wèn)Employee中的get_age(int ID)方法,并接受相應(yīng)的返回值
int age = 27;
return age;
}
double EmployeeProxy::get_salary(int ID)
{
// ...假定此處通過(guò)socket或者RPC等其他方式訪問(wèn)Employee中的get_salary(int ID)方法,并接受相應(yīng)的返回值
double salary = 50000.1;
return salary;
}
EmployeeProxy::~EmployeeProxy()
{
cout << "in the destructor of EmployeeProxy..." << endl;
}
// Proxy.cpp
#include "Proxy.h"
int main(int argc, char **argv)
{
IEmployee *employee = new EmployeeProxy;
cout << employee->get_name(10) << endl;
cout << employee->get_age(10) << endl;
cout << employee->get_salary(10) << endl;
delete employee;
return 0;
}
輸出結(jié)果:
玄機(jī)逸士
27
50000.1
in the destructor of EmployeeProxy…
in the destructor of IEmployee…
說(shuō)明:
- IEmployee是接口,相當(dāng)于圖3中的Service;
- Employee是在遠(yuǎn)程地址空間中對(duì)接口IEmployee的實(shí)現(xiàn);
- EmployeeProxy是在本地地址空間中對(duì)IEmployee的實(shí)現(xiàn),同時(shí)它通過(guò)網(wǎng)絡(luò)依賴Employee
- main函數(shù)(HRSystem)是客戶程序。它向EmployeeProxy發(fā)出請(qǐng)求,EmployeeProxy得到HRSystem發(fā)出的請(qǐng)求后,通過(guò)網(wǎng)絡(luò)將請(qǐng)求提交給遠(yuǎn)程的Employee,Employee接受請(qǐng)求并做出相關(guān)處理,將處理的結(jié)果返回給EmployeeProxy,EmployeeProxy將結(jié)果返回給HRSystem。
圖4
* 要完整實(shí)現(xiàn)一個(gè)Remote Proxy需要比較多的代碼,在此就用程序中的部分注釋來(lái)代替了。
Virtual Proxy C++示例代碼:
// Proxy.h
#define FILENAME "contacts.cbf"
// 一個(gè)簡(jiǎn)單的聯(lián)系人類
class Contact
{
private:
string name;
string email;
public:
Contact()
{
}
Contact(string name, string email) : name(name), email(email)
{
}
~Contact()
{
cout << "in the destructor of Contact..." << endl;
}
public:
// 下面幾個(gè)成員函數(shù)都是getter
string get_name() const
{
return name;
}
string get_email() const
{
return email;
}
};
// 聯(lián)絡(luò)本:一個(gè)抽象類,用作接口,它規(guī)定了聯(lián)絡(luò)本的相關(guān)方法
class IContactBook
{
protected:
public:
virtual void add(const Contact& contact) = 0; // 增加一個(gè)聯(lián)系人
virtual vector<Contact> get_contact(const string& name) = 0; // 查詢聯(lián)系人,有可能重名
virtual multimap<string, Contact> get_all_contacts() = 0; // 獲取所有聯(lián)系人
virtual void save() = 0; // 保存數(shù)據(jù)
public:
virtual ~IContactBook()
{
cout << "in the destructor of IContactBook..." << endl;
}
};
// ContactBook繼承IContactBook,該類實(shí)現(xiàn)了IContactBook中所有規(guī)定的方法,
// 它是一個(gè)“real object”(真實(shí)對(duì)象),將由ContactBookProxy來(lái)代理
class ContactBook : public IContactBook
{
private:
multimap<string, Contact> contacts;
public:
ContactBook()
{
Contact *contact = new Contact;
ifstream file(FILENAME, ios::in|ios::binary); // 打開(kāi)文件
if(!file)
{
cout << "can not open file..." << endl;
return;
}
Else // 將文件中的數(shù)據(jù)讀入contacts
{
file.seekg (0, ios::end);
int length = file.tellg();
file.seekg (0, ios::beg);
int element_number = length / sizeof(Contact);
cout << "element_number = " << element_number << endl;
for(int i = 0; i < element_number; i++)
{
file.seekg(sizeof(Contact) * i, ios::beg);
file.read((char *)contact, sizeof(Contact));
contacts.insert(pair<string, Contact>(contact->get_name(), *contact));
}
}
file.close();
delete contact;
contact = 0;
}
~ContactBook()
{
//save();
cout << "in the destructor of ContactBook..." << endl;
}
public:
// 根據(jù)名字獲取聯(lián)系人,由于有可能重名,因此返回值是一個(gè)vector<Contact>
vector<Contact> get_contact(const string& name)
{
multimap<string, Contact>::iterator it;
pair<multimap<string, Contact>::iterator, multimap<string, Contact>::iterator> ret;
ret = contacts.equal_range(name);
vector<Contact> contact_found;
for(it = ret.first; it != ret.second; it++)
{
contact_found.push_back(it->second);
}
return contact_found;
}
// 增加一個(gè)聯(lián)系人
void add(const Contact& contact)
{
string email;
vector<Contact> vcontact = get_contact(contact.get_name());
// 如果multimap contacts中不存在該聯(lián)系人
if(vcontact.empty())
{
// 則將聯(lián)系人contact加入到multimap中,聯(lián)系人contact的name作為key
contacts.insert(pair<string, Contact>(contact.get_name(), contact));
}
else // 同名的聯(lián)系人
{
email = contact.get_email();
int i = 0;
for(vector<Contact>::iterator it = vcontact.begin(); it != vcontact.end(); it++)
{
if(!(email == it->get_email())) // 同名的聯(lián)系人,但email不同。在這里,
{ // 我們假定email是唯一的,實(shí)際情況大多如此
i++;
}
}
if(i == vcontact.size()) // 和找出來(lái)所有同名的人的email地址都不一樣
{
contacts.insert(pair<string, Contact>(contact.get_name(), contact));
return;
}
}
}
// 將contacts中的所有聯(lián)系人存入到文件中
void save()
{
ofstream file(FILENAME, ios::out|ios::binary);
if(!file)
{
cout << "can not open file..." << endl;
return;
}
for(multimap<string, Contact>::iterator it = contacts.begin(); it != contacts.end(); it++)
{
file.write((const char *)(&(it->second)), sizeof(Contact));
}
file.close();
}
// 獲取所有聯(lián)系人
multimap<string, Contact> get_all_contacts()
{
return contacts;
}
// 一個(gè)輔助函數(shù),將被Proxy調(diào)用
void set_contacts(const multimap<string, Contact>& lcontacts)
{
contacts.erase(contacts.begin(), contacts.end());
contacts = lcontacts;
}
};
// 代理
class ContactBookProxy : public IContactBook
{
private:
IContactBook *contact_book; // 存放真實(shí)對(duì)象
multimap<string, Contact> local_contacts; // 本地聯(lián)系人數(shù)據(jù)
public:
ContactBookProxy()
{
contact_book = new ContactBook; // 創(chuàng)建真實(shí)對(duì)象
local_contacts = contact_book->get_all_contacts(); // 將真實(shí)對(duì)象中的數(shù)據(jù)取到本地
delete contact_book; // 取出數(shù)據(jù)后,立即銷毀真實(shí)對(duì)象
contact_book = 0; // 此后,均指操作本地聯(lián)系人數(shù)據(jù)local_contacts
}
~ContactBookProxy()
{
cout << "in the destructor of ContactBookProxy..." << endl;
if(!contact_book)
{
delete contact_book;
contact_book = 0;
}
}
vector<Contact> get_contact(const string& name)
{
// 只操作本地?cái)?shù)據(jù)local_contacts,因?yàn)榇藭r(shí)local_contacts已經(jīng)包含了真實(shí)對(duì)象中的所有數(shù)據(jù)
multimap<string, Contact>::iterator it;
pair<multimap<string, Contact>::iterator, multimap<string, Contact>::iterator> ret;
ret = local_contacts.equal_range(name);
vector<Contact> contact_found;
for(it = ret.first; it != ret.second; it++)
{
contact_found.push_back(it->second);
}
return contact_found;
}
void add(const Contact& contact)
{
string email;
vector<Contact> vcontact = get_contact(contact.get_name());
// 如果multimap contacts中不存在該聯(lián)系人
if(vcontact.empty())
{
// 則將聯(lián)系人contact加入到multimap中,聯(lián)系人contact的name作為key
local_contacts.insert(pair<string, Contact>(contact.get_name(), contact));
}
else // 同名的聯(lián)系人
{
email = contact.get_email();
int i = 0;
for(vector<Contact>::iterator it = vcontact.begin(); it != vcontact.end(); it++)
{
if(!(email == it->get_email())) // 同名的聯(lián)系人,但email不同。在這里,
{ // 我們假定email是唯一的,實(shí)際情況大多如此
i++;
}
}
if(i == vcontact.size()) // 和找出來(lái)所有同名的人的email地址都不一樣
{
local_contacts.insert(pair<string, Contact>(contact.get_name(), contact));
return;
}
}
}
void save()
{
if(!contact_book)
{
contact_book = new ContactBook;
}
ContactBook *cb = dynamic_cast<ContactBook*>(contact_book); // down casting
cb->set_contacts(local_contacts);
contact_book->save();
}
multimap<string, Contact> get_all_contacts()
{
return local_contacts;
}
};
// Proxy.cpp
int main(int argc, char **argv)
{
IContactBook *contact_proxy = new ContactBookProxy;
contact_proxy->add(Contact("玄機(jī)逸士", "xjys@pnft.com"));
contact_proxy->add(Contact("玄機(jī)逸士", "xjys@pnft.com"));
contact_proxy->add(Contact("上官天野", "sgty@pnft.com"));
contact_proxy->add(Contact("上官天野", "sgty1@pnft.com"));
contact_proxy->add(Contact("上官天野", "sgty2@pnft.com"));
contact_proxy->add(Contact("黃家四娘", "hjsn@pnft.com"));
contact_proxy->save();
vector<Contact> vec_contact = contact_proxy->get_contact("上官天野");
for(vector<Contact>::const_iterator it = vec_contact.begin(); it != vec_contact.end(); it++)
{
cout << it->get_name() << " : " << it->get_email() << endl;
}
cout << "------------------------------------" << endl;
multimap<string, Contact> map_all_contacts(contact_proxy->get_all_contacts());
for(multimap<string, Contact>::iterator it = map_all_contacts.begin(); it != map_all_contacts.end(); it++)
{
cout << it->first << " : " << (it->second).get_email() << endl;
}
delete contact_proxy;
return 0;
}
輸出結(jié)果:
element_number = 5
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of ContactBook...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of IContactBook...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
element_number = 5
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
上官天野 : sgty@pnft.com
上官天野 : sgty1@pnft.com
上官天野 : sgty2@pnft.com
------------------------------------
黃家四娘 : hjsn@pnft.com
上官天野 : sgty@pnft.com
上官天野 : sgty1@pnft.com
上官天野 : sgty2@pnft.com
玄機(jī)逸士 : xjys@pnft.com
in the destructor of ContactBookProxy...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of IContactBook...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
in the destructor of Contact...
上述代碼的要點(diǎn)是:首先將真實(shí)對(duì)象中的數(shù)據(jù)拷貝到代理,并立即銷毀真實(shí)對(duì)象,然后客戶端程序一直對(duì)代理進(jìn)行操作,直到所需要的操作完成后,將代理中的結(jié)果返回給真實(shí)對(duì)象,并由真實(shí)對(duì)象保存到文件中。嚴(yán)格地說(shuō),這個(gè)示例代碼并不是真正意義上的Virtual Proxy,因?yàn)樗](méi)有延遲真實(shí)對(duì)象的創(chuàng)建。不過(guò)要實(shí)現(xiàn)真正意義上Virtual Proxy也并不困難。
Protection proxy的示例代碼,和前面的代碼差不多,只是在proxy的成員函數(shù)在調(diào)用與之對(duì)應(yīng)的真實(shí)對(duì)象的成員函數(shù)之前,進(jìn)行保護(hù)性檢查。如果通過(guò)了檢查則調(diào)用真實(shí)對(duì)象的對(duì)應(yīng)成員函數(shù),否則,不調(diào)用并提示相關(guān)信息。具體代碼略。