代理模式:为其他对象提供一种代理以控制对这个对象的访问。大话模设计模式里边提供了一个有趣的事例,A想追求mm,给这个mm送各种礼物,但他选取B 来替他完成送礼物这件事。这个mm知道礼物是A送的,但她没见过A这个人,B是追求者A和mm的中介来实际完成追求这件事情,他知道A的存在,并依赖于A买的礼物来代替A追求mm。结果当然是A悲剧了,假手他人终无望,需知此事须躬行。
UML类图:
#include <iostream>
using namespace std;
class GiveGift
{
public:
virtual void GiveDolls() = 0;
virtual void GiveFlowers() = 0;
virtual void GiveChocolate() = 0;
};
class pursuit : public GiveGift
{
public:
pursuit() {cout<<"Pursuit Constructed\n";}
void GiveDolls() {cout<<"Give Dolls\n";}
void GiveFlowers() {cout<<"Give Flowers\n";}
void GiveChocolate() {cout<<"Give Chocolate\n";}
};
class proxy : public GiveGift
{
public:
pursuit *psut;
public:
proxy() {psut = new pursuit();}
~proxy() {if(!psut) delete psut;}
void GiveDolls() {psut->GiveDolls();}
void GiveFlowers() {psut->GiveFlowers();}
void GiveChocolate() {psut->GiveChocolate();}
};
int main()
{
proxy * pxy;
pxy = new proxy();
pxy->GiveDolls();
pxy->GiveFlowers();
pxy->GiveChocolate();
if(!pxy)
delete pxy;
return 0;
}
版权声明:本文为aa1059258042原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。