首页 > 代码库 > 代理模式之C++实现
代理模式之C++实现
#include "stdafx.h"
#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;
#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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。