【1】什么是代理模式?
为其他对象提供一种代理,并以控制对这个对象的访问。
【2】代理模式代码示例:
示例代码:
#include <iostream>
#include <string>
using namespace std;
class SchoolGirl
{
public:
string name;
};
/*
* 接口
*/
class IGiveGift
{
public:
virtual void giveDolls() = 0;
virtual void giveFlowers() = 0;
};
/*
* 委托类
*/
class Pursuit : public IGiveGift
{
private:
SchoolGirl mm;
public:
Pursuit(SchoolGirl m)
{
mm = m;
}
void giveDolls()
{
cout << mm.name << " 送你娃娃" << endl;
}
void giveFlowers()
{
cout << mm.name << " 送你鲜花" << endl;
}
};
/*
* 代理类
*/
class Proxy : public IGiveGift
{
private:
Pursuit gg;
public:
Proxy(SchoolGirl mm) : gg(mm)
{
}
void giveDolls()
{
gg.giveDolls();
}
void giveFlowers()
{
gg.giveFlowers();
}
};
/*
* 客户端
*/
int main()
{
SchoolGirl lijiaojiao;
lijiaojiao.name = "李娇娇";
Pursuit zhuojiayi(lijiaojiao);
Proxy daili(lijiaojiao);
daili.giveDolls();
daili.giveFlowers();
return 0;
}
https://blog.csdn.net/f2764052703/article/details/94558263
https://www.jianshu.com/p/9cdcf4e5c27d