代理模式之C++实现
#include <iostream>
#include <string>
#include <map>
using namespace std;
class ForeignGoods
{
private:
string name;
public:
ForeignGoods(string name)
{
this->name = name;
}
string GetName()
{
return this->name;
}
};
class Purchase
{
public:
virtual void Buy() = 0;
};
class Customer : public Purchase
{
private:
ForeignGoods *pGoods;
public:
Customer(ForeignGoods *goods)
{
pGoods = goods;
}
void Buy()
{
cout << "购买" << pGoods->GetName() << endl;
}
};
class Proxy : public Purchase
{
private:
Customer *pCustomer;
public:
Proxy(ForeignGoods *pGoods)
{
pCustomer = new Customer(pGoods);
}
void Buy()
{
pCustomer->Buy();
}
};
int main(void)
{
ForeignGoods *pGoods = new ForeignGoods("香奈儿香水");
Proxy *pProxy = new Proxy(pGoods);
pProxy->Buy();
free(pProxy);
free(pGoods);
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。