青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

積木

No sub title

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

轉載自:http://patmusing.blog.163.com/blog/static/13583496020101410329439/


Proxy also a.k.a. Surrogate

對于復雜的軟件系統常常有一種處理手法,即增加一層間接層,從而使得系統獲得一種更為靈活、滿足特定需求的解決方案。在面向對象的系統中,有些對象由于某種原因,比如對象創建的開銷很大,或者某些操作需要安全控制,或者需要進程外的訪問等,直接訪問會給使用者或者系統結構帶來很多麻煩。

Proxy設計模式就是在不失去透明操作對象的同時,通過增加一層間接層來管理、控制這些對象特有的復雜性。

14. C++實現Structural - Proxy模式  - 玄機逸士 - 玄機逸士博客

1

14. C++實現Structural - Proxy模式  - 玄機逸士 - 玄機逸士博客

2

表示不同的進程

1中假定AB位于不同的進程,典型的情況是AB位于互聯網上兩臺不同的機器上,如果A要訪問B三次,那么A就要如上圖調用B三次。

2中增加一個B的代理,該代理和A位于同一個進程內,當A要調用B的功能時,A僅需要調用B的代理即可,然后B的代理和B之間進行通信。另外如果A3次調用B的功能,那么A僅需要3此調用B的代理即可,而B的代理和B之間有可能只需要一次通信就夠了,由此可見,B的代理有一定的緩沖功能。

Provide a surrogate or placeholder for another object to control access to it. (為其他對象提供一種代理以控制對這個對象的訪問) -GoF

Proxy設計模式之靜態結構UML類圖:

14. C++實現Structural - Proxy模式  - 玄機逸士 - 玄機逸士博客

3

說明:

Service

The interface that both the proxy and the real object will implement. (代理和實際對象必須實現的接口)

ServiceProxy

ServiceProxy implements Service and forwards method calls to the real object (ServiceImplemention) when appropriate.

(ServiceProxy實現了Service接口,并在合適的時候,將請求傳遞給實際對象ServiceImplementation)

ServiceImplementation

The real, full implementation of the interface. This object will be represented by the Proxy object.

(真正、完整的接口實現,該對象由Proxy對象代表)

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)

{

// ... 假定此處查詢數據庫,獲得ID對應員工的姓名

string name = "玄機逸士";

return name;

}

int Employee::get_age(int ID)

{

// ... 假定此處查詢數據庫,獲得ID對應員工的年齡

int age = 27;

return age;

}

double Employee::get_salary(int ID)

{

// ... 假定此處查詢數據庫,獲得ID對應員工的工資

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)

{

// ...假定此處通過socket或者RPC等其他方式訪問Employee中的get_name(int ID)方法,并接受相應的返回值

string name = "玄機逸士";

return name;

}

int EmployeeProxy::get_age(int ID)

{

// ...假定此處通過socket或者RPC等其他方式訪問Employee中的get_age(int ID)方法,并接受相應的返回值

int age = 27;

return age;

}

double EmployeeProxy::get_salary(int ID)

{

// ...假定此處通過socket或者RPC等其他方式訪問Employee中的get_salary(int ID)方法,并接受相應的返回值

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;

}

輸出結果:

玄機逸士

27

50000.1

in the destructor of EmployeeProxy…

in the destructor of IEmployee…

說明:

- IEmployee是接口,相當于圖3中的Service

- Employee是在遠程地址空間中對接口IEmployee的實現;

- EmployeeProxy是在本地地址空間中對IEmployee的實現,同時它通過網絡依賴Employee

- main函數(HRSystem)是客戶程序。它向EmployeeProxy發出請求,EmployeeProxy得到HRSystem發出的請求后,通過網絡將請求提交給遠程的EmployeeEmployee接受請求并做出相關處理,將處理的結果返回給EmployeeProxyEmployeeProxy將結果返回給HRSystem

14. C++實現Structural - Proxy模式  - 玄機逸士 - 玄機逸士博客

4

* 要完整實現一個Remote Proxy需要比較多的代碼,在此就用程序中的部分注釋來代替了。

Virtual Proxy C++示例代碼

// Proxy.h

#define FILENAME "contacts.cbf"

// 一個簡單的聯系人類

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:

// 下面幾個成員函數都是getter

string get_name() const

{

return name;

}

string get_email() const

{

return email;

}

};

// 聯絡本:一個抽象類,用作接口,它規定了聯絡本的相關方法

class IContactBook

{

protected:

public:

virtual void add(const Contact& contact) = 0; // 增加一個聯系人

virtual vector<Contact> get_contact(const string& name) = 0; // 查詢聯系人,有可能重名

virtual multimap<string, Contact> get_all_contacts() = 0; // 獲取所有聯系人

virtual void save() = 0; // 保存數據

public:

virtual ~IContactBook()

{

cout << "in the destructor of IContactBook..." << endl;

}

};

// ContactBook繼承IContactBook,該類實現了IContactBook中所有規定的方法,

// 它是一個“real object”(真實對象),將由ContactBookProxy來代理

class ContactBook : public IContactBook

{

private:

multimap<string, Contact> contacts;

public:

ContactBook()

{

Contact *contact = new Contact;

ifstream file(FILENAME, ios::in|ios::binary); // 打開文件

if(!file)

{

cout << "can not open file..." << endl;

return;

}

Else // 將文件中的數據讀入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:

// 根據名字獲取聯系人,由于有可能重名,因此返回值是一個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;

}

// 增加一個聯系人

void add(const Contact& contact)

{

string email;

vector<Contact> vcontact = get_contact(contact.get_name());

// 如果multimap contacts中不存在該聯系人

if(vcontact.empty())

{

// 則將聯系人contact加入到multimap中,聯系人contactname作為key

contacts.insert(pair<string, Contact>(contact.get_name(), contact));

}

else // 同名的聯系人

{

email = contact.get_email();

int i = 0;

for(vector<Contact>::iterator it = vcontact.begin(); it != vcontact.end(); it++)

{

if(!(email == it->get_email())) // 同名的聯系人,但email不同。在這里,

{ // 我們假定email是唯一的,實際情況大多如此

i++;

}

}

if(i == vcontact.size()) // 和找出來所有同名的人的email地址都不一樣

{

contacts.insert(pair<string, Contact>(contact.get_name(), contact));

return;

}

}

}

// contacts中的所有聯系人存入到文件中

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();

}

// 獲取所有聯系人

multimap<string, Contact> get_all_contacts()

{

return contacts;

}

// 一個輔助函數,將被Proxy調用

void set_contacts(const multimap<string, Contact>& lcontacts)

{

contacts.erase(contacts.begin(), contacts.end());

contacts = lcontacts;

}

};

// 代理

class ContactBookProxy : public IContactBook

{

private:

IContactBook *contact_book; // 存放真實對象

multimap<string, Contact> local_contacts; // 本地聯系人數據

public:

ContactBookProxy()

{

contact_book = new ContactBook; // 創建真實對象

local_contacts = contact_book->get_all_contacts(); // 將真實對象中的數據取到本地

delete contact_book; // 取出數據后,立即銷毀真實對象

contact_book = 0; // 此后,均指操作本地聯系人數據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)

{

// 只操作本地數據local_contacts,因為此時local_contacts已經包含了真實對象中的所有數據

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中不存在該聯系人

if(vcontact.empty())

{

// 則將聯系人contact加入到multimap中,聯系人contactname作為key

local_contacts.insert(pair<string, Contact>(contact.get_name(), contact));

}

else // 同名的聯系人

{

email = contact.get_email();

int i = 0;

for(vector<Contact>::iterator it = vcontact.begin(); it != vcontact.end(); it++)

{

if(!(email == it->get_email())) // 同名的聯系人,但email不同。在這里,

{ // 我們假定email是唯一的,實際情況大多如此

i++;

}

}

if(i == vcontact.size()) // 和找出來所有同名的人的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("玄機逸士", "xjys@pnft.com"));

contact_proxy->add(Contact("玄機逸士", "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;

}

輸出結果:

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

玄機逸士 : 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...

上述代碼的要點是:首先將真實對象中的數據拷貝到代理,并立即銷毀真實對象,然后客戶端程序一直對代理進行操作,直到所需要的操作完成后,將代理中的結果返回給真實對象,并由真實對象保存到文件中。嚴格地說,這個示例代碼并不是真正意義上的Virtual Proxy,因為它并沒有延遲真實對象的創建。不過要實現真正意義上Virtual Proxy也并不困難。

Protection proxy的示例代碼,和前面的代碼差不多,只是在proxy的成員函數在調用與之對應的真實對象的成員函數之前,進行保護性檢查。如果通過了檢查則調用真實對象的對應成員函數,否則,不調用并提示相關信息。具體代碼略。



posted on 2013-03-07 23:43 Jacc.Kim 閱讀(266) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲精品专区| 久久视频在线视频| 亚洲一区二区精品视频| 欧美日韩午夜| 一区二区欧美日韩视频| 夜夜爽av福利精品导航 | 欧美日韩精品二区第二页| 亚洲国内高清视频| 亚洲精品裸体| 欧美精品二区| 一区二区三区鲁丝不卡| 亚洲免费视频网站| 国产精品影音先锋| 欧美一区二区在线免费播放| 久久久久久欧美| 亚洲国产欧美不卡在线观看| 欧美福利专区| aⅴ色国产欧美| 午夜精品视频网站| 国产亚洲精品一区二555| 欧美在线亚洲在线| 免费国产一区二区| 一区二区欧美在线| 国产精品毛片大码女人 | 欧美国产综合| 99精品热视频| 久久激情五月激情| 一区二区亚洲| 欧美国产精品久久| 一区二区三区欧美| 久久亚洲午夜电影| 亚洲伦伦在线| 国产免费成人| 美日韩精品视频| 99精品久久久| 久久精品视频网| 日韩视频一区二区三区在线播放| 欧美三级视频| 久久精品日韩欧美| 亚洲高清在线| 久久国产精品一区二区三区| 亚洲国产精品v| 国产精品高清免费在线观看| 久久精品一区中文字幕| 亚洲美女啪啪| 久久亚洲春色中文字幕| 夜夜嗨av一区二区三区中文字幕| 国产丝袜美腿一区二区三区| 欧美黄免费看| 欧美一级欧美一级在线播放| 亚洲国产美女| 久久久91精品国产一区二区精品| 亚洲精品在线一区二区| 国产欧美一区二区三区久久| 欧美精品一区二区视频| 欧美一区二区三区视频免费播放 | 韩国三级电影久久久久久| 欧美激情中文字幕乱码免费| 欧美一区二区啪啪| 亚洲精品偷拍| 欧美freesex8一10精品| 欧美一区综合| 亚洲午夜精品久久久久久浪潮| 尤物视频一区二区| 国产伦精品一区| 欧美日韩中文另类| 美女在线一区二区| 欧美一区二区免费观在线| 一区二区三区成人精品| 亚洲大胆女人| 免费看的黄色欧美网站| 欧美专区在线观看| 亚洲一区免费| 中文在线资源观看网站视频免费不卡 | 亚洲在线观看免费| 最新亚洲一区| 精品999在线观看| 国产精品自在欧美一区| 欧美日韩国产综合视频在线观看中文 | 欧美大片一区二区| 久久精品国产一区二区电影 | 麻豆精品视频| 久久久久五月天| 性一交一乱一区二区洋洋av| 中文av一区二区| 亚洲每日更新| 亚洲人成网在线播放| 一区二区三区在线不卡| 国产揄拍国内精品对白| 国产女人aaa级久久久级| 国产精品专区第二| 国产精品三上| 国产精品一区二区久久国产| 国产精品久久久久久久久久ktv| 欧美日韩国产一级片| 欧美精品久久久久a| 欧美精品1区| 欧美日韩成人在线| 欧美日韩亚洲成人| 欧美日韩在线三区| 国产精品高潮呻吟久久| 国产精品一区亚洲| 国产亚洲高清视频| 黄色工厂这里只有精品| 国外成人性视频| 影音先锋久久资源网| 亚洲大片在线| 亚洲精品国精品久久99热| 亚洲电影在线| 日韩视频一区二区三区| 亚洲在线成人| 久久精品国产亚洲一区二区| 狼人社综合社区| 欧美成年人在线观看| 亚洲青涩在线| 亚洲午夜视频在线观看| 午夜久久美女| 久久亚洲私人国产精品va| 欧美高清成人| 国产精品国产三级国产a| 国产目拍亚洲精品99久久精品| 韩国av一区| 日韩亚洲国产欧美| 午夜精品久久久久| 久久在线免费观看视频| 亚洲精品国产无天堂网2021| 一本色道久久综合一区| 欧美一二三视频| 欧美二区在线播放| 国产精品久久久久久久久久尿| 国产一区二区你懂的| 亚洲精品国产精品乱码不99 | 欧美成ee人免费视频| 亚洲人久久久| 午夜精品视频在线观看| 欧美电影免费观看大全| 国产精品入口| 亚洲国内自拍| 午夜精品短视频| 欧美国产激情| 亚洲中字在线| 欧美freesex交免费视频| 国产精品美女www爽爽爽| 亚洲二区在线| 欧美亚洲综合久久| 亚洲国产精品一区二区第一页 | 欧美黄色网络| 国产一本一道久久香蕉| 99天天综合性| 开元免费观看欧美电视剧网站| 日韩网站在线看片你懂的| 久久国产视频网| 国产精品久久久久高潮| 91久久久久| 久久久精品国产一区二区三区 | 国产精品毛片大码女人| 亚洲国产成人在线播放| 性做久久久久久久免费看| 最新日韩在线视频| 久久精品国产91精品亚洲| 国产精品国产自产拍高清av| 亚洲人成毛片在线播放| 久久一区激情| 亚洲欧美日韩一区二区在线| 欧美日韩的一区二区| 在线看片日韩| 久久精品男女| 亚洲欧美大片| 欧美吻胸吃奶大尺度电影| 亚洲人成人99网站| 免费欧美日韩| 久久99伊人| 国产欧美一区视频| 亚洲伊人久久综合| 亚洲精品欧美精品| 另类图片国产| 激情一区二区三区| 久久精品人人| 欧美一区二区女人| 国产欧美韩国高清| 亚洲欧美一区二区三区在线| 亚洲免费不卡| 欧美日韩国产小视频| 亚洲人成啪啪网站| 亚洲第一福利视频| 免费欧美电影| 亚洲精品国产精品国产自| 欧美黄色一区二区| 免费观看国产成人| 亚洲激情视频| 亚洲国产精品高清久久久| 欧美freesex交免费视频| 亚洲人精品午夜| 亚洲啪啪91| 欧美人成在线| 一区二区三区四区五区在线| 亚洲靠逼com| 欧美午夜欧美| 午夜亚洲一区| 欧美亚洲综合久久|