代理模式(Proxy)
為其他對象提供一種代理以控制對這個對象的訪問。
// ProxyTest01.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <string>using namespace std;//想要追求的姑娘class SchoolGirl{public : string name;};//贈送禮物class IGiveGift{public : virtual void GiveDolls() = 0; virtual void GiveFlowers() = 0; virtual void GiveChocolate() = 0;};//追求者,具體表現是送給喜歡的姑娘禮物class Pursuit : public IGiveGift{private : SchoolGirl mm;public : //Pursuit(){} Pursuit(SchoolGirl mm) { this->mm = mm; }public : void GiveDolls() { cout<<mm.name<<":送你洋娃娃"<<endl; } void GiveFlowers() { cout<<mm.name<<":送你鮮花"<<endl; } void GiveChocolate() { cout<<mm.name<<":送你巧克力"<<endl; }};//代理,幫追求者來做事,表現也是送東西給姑娘,當然是代追求者送的class Proxy : public IGiveGift{private : Pursuit *pursuit;public : ~Proxy(){ delete pursuit; } Proxy(SchoolGirl mm) { pursuit = new Pursuit(mm); }public : void GiveDolls() { pursuit->GiveDolls(); } void GiveFlowers() { pursuit->GiveFlowers(); } void GiveChocolate() { pursuit->GiveChocolate(); }};int _tmain(int argc, _TCHAR* argv[]){ SchoolGirl mm; mm.name = "美女"; //追求者親自來追女生 //Pursuit p(mm); //p.GiveDolls(); //p.GiveFlowers(); //p.GiveChocolate(); //代理來代某個人來追女生 Proxy proxy(mm); proxy.GiveDolls(); proxy.GiveFlowers(); proxy.GiveChocolate(); system("pause"); return 0;}