设计模式之Proxy代理模式

1. 远程代理 

/*
* 第一种代理模式:远程代理

*(情景:小明喜欢小红,但是害羞不好意思表白。小明通过中间代理人小丽,把礼物送给小红,表达自己对小红的爱意)
*/

Subject抽象主题角色

//实现一个抽象的女孩类
class Girl
{
public:
	Girl(char* name = "") :m_name(name)
	{

	}
	char* getName()
	{
		return m_name;
	}

private:
	char* m_name;
};

//实现一个送礼物的接口类
class GiveGift
{
public:
	virtual void GiveDolls() = 0;
	virtual void GiveFlowers() = 0;
	virtual void GiveChocolate() = 0;
};

RealSubject指针主体角色

//送礼物的实例类,比如追求者小明
class Puisuit : public GiveGift
{
public:
	Puisuit(char* name, Girl loveGirl) :m_SelfName(name), m_LoveGirl(loveGirl)
	{
	}
	char* getSelfName()
	{
		return m_SelfName;
	}

	char* getLoveGirlName()
	{
		return m_LoveGirl.getName();
	}
	
	virtual void GiveDolls()
	{
		std::cout << "送" << m_LoveGirl.getName() << "玩具!" << std::endl;

	}
	virtual void GiveFlowers()
	{
		std::cout << "送" << m_LoveGirl.getName() << "鲜花!" << std::endl;
	}
	virtual void GiveChocolate()
	{
		std::cout << "送" << m_LoveGirl.getName() << "巧克力!" << std::endl;
	}
private:
	Girl m_LoveGirl;//被追求对象:小红
	char* m_SelfName;
};

Proxy代理主题角色

//送礼物的代理类,比如中间人小丽
class Proxy : public GiveGift
{
public:
	Proxy(char* name, Girl girl)
	{
		m_Puisuit = new Puisuit(name,girl);
	}
	~Proxy()
	{
		if (nullptr != m_Puisuit)
		{
			delete m_Puisuit;
			m_Puisuit = nullptr;
		}
	}
	virtual void GiveDolls()
	{
		std::cout << m_Puisuit->getLoveGirlName() << ",这是" << m_Puisuit->getSelfName() << "送你的礼物!" << std::endl;

		m_Puisuit->GiveDolls();
	}
	virtual void GiveFlowers()
	{
		std::cout << m_Puisuit->getLoveGirlName() << ",这是" << m_Puisuit->getSelfName() << "送你的礼物!" << std::endl;
		m_Puisuit->GiveFlowers();
	}
	virtual void GiveChocolate()
	{
		std::cout << m_Puisuit->getLoveGirlName() << ",这是" << m_Puisuit->getSelfName() << "送你的礼物!" << std::endl;
		m_Puisuit->GiveChocolate();
	}
private:
	Puisuit* m_Puisuit;//追求对象:小明
};

主函数

int main()
{
	std::cout << "***************远程代理***************" << std::endl;
	Girl loveGirl("小红");

	//中间人小丽
	Proxy pro("小明", loveGirl);

	//小丽代表小明给小丽送玩具
	pro.GiveDolls();
	std::cout << std::endl;
	
	//小丽代表小明给小丽送鲜花
	pro.GiveFlowers();
	std::cout << std::endl;

	//小丽代表小明给小丽送巧克力
	std::cout << std::endl;
	pro.GiveChocolate();

	system("pause");
	return 0;
}

2. 虚拟代理

 /*
* 第二种代理模式:虚拟代理(根据需要创建开销最大的对象,通过它来存放实例化需要很长时间的对象)
*/

//抽象主题角色
class Image
{
public:
	Image(std::string name) : m_imageName(name){}
	virtual ~Image(){}
	virtual void Show(){}

protected:
	string m_imageName;
};

//真实主题角色
class BigImage : public Image
{
public:
	BigImage(std::string name) : Image(name){}
	~BigImage() {}
	void Show(){}

private:
};

//代理主题角色
class BigImageProxy : public Image
{
public:
	BigImageProxy(std::string name) : Image(name), m_pBigImage(nullptr)
	{

	}
	~BigImageProxy()
	{
		if (nullptr != m_pBigImage)
		{
			delete m_pBigImage;
			m_pBigImage = nullptr;
		}
	}

	void Show()
	{
		if (nullptr == m_pBigImage)
		{
			m_pBigImage = new BigImage(m_imageName);
		}
		m_pBigImage->Show();
	}

private:
	BigImage* m_pBigImage;
};

//主函数
int main()
{
	std::cout << "***************虚拟代理***************" << std::endl;
	Image *image = new BigImageProxy("proxy.jpg"); //代理  
	image->Show(); //需要时由代理负责打开  
	delete image;
	std::cout << std::endl;

	system("pause");
	return 0;
}

3. 安全代理

/*
* 第三种代理模式:安全代理(游戏中,通过代理来控制不同vip玩家的游戏权限)
*/ 

//抽象类
class Play
{
public:
	virtual void Play1() = 0;
	virtual void Play2() = 0;
	virtual void Play3() = 0;

private:

};

//操作类
class Player : public Play
{
public:
	void Play1()
	{
		std::cout << "Play1:战役" << std::endl;
	}
	void Play2()
	{
		std::cout << "Play2:军团" << std::endl;
	}	
	void Play3()
	{
		std::cout << "Play3:神器" << std::endl;
	}
private:

};

//不用玩家的代理
class ProxyPlayerVIP1 : public Play
{
public:
	ProxyPlayerVIP1()
	{
		m_pPlayer = new Player;
	}
	void Play1()
	{
		m_pPlayer->Play1();
	}
	void Play2()
	{
		std::cout << "Play2:没有权限!" << std::endl;
	}
	void Play3()
	{
		std::cout << "Play3:没有权限!" << std::endl;
	}

private:
	Play* m_pPlayer;
};

class ProxyPlayerVIP2 : public Play
{
public:
	ProxyPlayerVIP2()
	{
		m_pPlayer = new Player;
	}
	void Play1()
	{
		std::cout << "Play1:没有权限!" << std::endl;
	}
	void Play2()
	{
		m_pPlayer->Play2();
	}
	void Play3()
	{
		std::cout << "Play3:没有权限!" << std::endl;
	}

private:
	Play* m_pPlayer;
};

class ProxyPlayerVIP3 : public Play
{
public:
	ProxyPlayerVIP3()
	{
		m_pPlayer = new Player;
	}
	void Play1()
	{
		std::cout << "Play1:没有权限!" << std::endl;
	}
	void Play2()
	{
		std::cout << "Play2:没有权限!" << std::endl;
	}
	void Play3()
	{
		m_pPlayer->Play3();
	}

private:
	Play* m_pPlayer;
};

//主函数
int main()
{
	std::cout << "***************安全代理***************" << std::endl;
	ProxyPlayerVIP1 pro1;
	pro1.Play1();
	pro1.Play2();
	pro1.Play3();
	std::cout << std::endl;

	ProxyPlayerVIP2 pro2;
	pro2.Play1();
	pro2.Play2();
	pro2.Play3();
	std::cout << std::endl;

	ProxyPlayerVIP3 pro3;
	pro3.Play1();
	pro3.Play2();
	pro3.Play3();

	system("pause");
	return 0;
}

4. 智能引用代理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖七少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值