大话设计模式(4)

代理模式

没有代理的代码
在这里插入图片描述

#include <iostream>
using namespace std;
#include <string>

class SchoolGirl {
private:
	string name;
public:
	void setName(string name) {
		this->name = name;
	}
	string getName() {
		return this->name;
	}
};

class Pursuit {
public:
	Pursuit(SchoolGirl* mm) {
		this->mm = mm;
	}
	void GiveDolls() {
		cout << mm->getName() << "送你洋娃娃" << endl;
	}
	void GiveFlowers() {
		cout << mm->getName() << "送你鲜花" << endl;
	}
	SchoolGirl* mm = NULL;
};

int main() {
	SchoolGirl* jiaojiao = new SchoolGirl;
	jiaojiao->setName("李娇娇");

	Pursuit* zhuojiayi = new Pursuit(jiaojiao);  //娇娇并不认识卓贾易,此处有问题

	zhuojiayi->GiveDolls();
	zhuojiayi->GiveFlowers();

	return 0;
}

只有代理的代码
在这里插入图片描述

#include <iostream>
using namespace std;
#include <string>

class SchoolGirl {
private:
	string name;
public:
	void setName(string name) {
		this->name = name;
	}
	string getName() {
		return this->name;
	}
};

class Proxy {
public:
	Proxy(SchoolGirl* mm) {
		this->mm = mm;
	}
	void GiveDolls() {
		cout << mm->getName() << "送你洋娃娃" << endl;
	}
	void GiveFlowers() {
		cout << mm->getName() << "送你鲜花" << endl;
	}
	SchoolGirl* mm = NULL;
};

int main() {
	SchoolGirl* jiaojiao = new SchoolGirl;
	jiaojiao->setName("李娇娇");

	Proxy* daili = new Proxy(jiaojiao);

	daili->GiveDolls();
	daili->GiveFlowers();

	return 0;
}

这里只是把 Pursuit(追求者)换成了 Proxy(代理者),这样就变成了礼物是代理者送的了,并没有将 SchoolGirl(被追求者)、Pursuit(追求者)、Proxy(代理者)三者联系起来。
在这里插入图片描述

#include <iostream>
using namespace std;
#include <string>

class SchoolGirl {
private:
	string name;
public:
	void setName(string name) {
		this->name = name;
	}
	string getName() {
		return this->name;
	}
};

//代理接口抽象类
class GiveGift {
public:
	virtual void GiveDolls() = 0;
	virtual void GiveFlowers() = 0;
};
//追求者类
class Pursuit :public GiveGift {
public:
	Pursuit(SchoolGirl* mm) {
		this->mm = mm;
	}
	void GiveDolls() {
		cout << mm->getName() << "送你洋娃娃" << endl;
	}
	void GiveFlowers() {
		cout << mm->getName() << "送你鲜花" << endl;
	}
	SchoolGirl* mm = NULL;
};
//代理类
class Proxy :public GiveGift {
public:
	Proxy(SchoolGirl* mm) {
		gg = new Pursuit(mm);
	}
	void GiveDolls() {
		gg->GiveDolls();   //在实现方法中去调用“追求者”类的相关方法
	}
	void GiveFlowers() {
		gg->GiveFlowers();
	}
	~Proxy() {
		if (gg != NULL) {
			delete gg;
		}
	}
public:
	Pursuit* gg;
};

int main() {
	SchoolGirl* jiaojiao = new SchoolGirl;
	jiaojiao->setName("李娇娇");

	Proxy* daili = new Proxy(jiaojiao);

	daili->GiveDolls();
	daili->GiveFlowers();

	return 0;
}

另附博文:c++之代理模式
在这里插入图片描述
实例1

#include <iostream>
using namespace std;

//抽象工厂
class IFactory
{
public:
	virtual ~IFactory() {}
	virtual void createProduct() = 0;
};

//苹果AirPods耳机工厂
class AppleAirpodsFactory :public IFactory
{
public:
	void createProduct()
	{
		cout << "生产苹果AirPods耳机" << endl;
	}
};

//立讯精密公司代理生产
class ILXCompanyProxy :public IFactory
{
public:
	ILXCompanyProxy() {
		m_factory = new AppleAirpodsFactory;
	}
	void createProduct()
	{
		cout << "立讯精密代理:" << endl;
		m_factory->createProduct();
	}
	~ILXCompanyProxy() {
		if (m_factory != NULL) {
			delete m_factory;
		}
	}
private:
	AppleAirpodsFactory* m_factory;
};

//客户端
int main()
{
	ILXCompanyProxy* proxy = new ILXCompanyProxy;
	proxy->createProduct();

	return 0;
}

实例2

#include <iostream>
using namespace std;
#include <string>

class AbstractCommonInterface {
public:
	virtual void run() = 0;
};
class MySystem :public AbstractCommonInterface {
public:
	virtual void run() {
		cout << "系统启动" << endl;
	}
};

class MySystemProxy :public AbstractCommonInterface {
public:
	MySystemProxy(string username, string password) {
		this->mUsername = username;
		this->mPassword = password;
		pSystem = new MySystem;
	}
	bool checkUsernameAndPassword() {
		if (mUsername == "admin" && mPassword == "admin") {
			return true;
		}
		return false;
	}
	virtual void run() {
		if (checkUsernameAndPassword()) {
			cout << "用户名和密码正确,验证通过" << endl;
			this->pSystem->run();			
		}
		else {
			cout << "用户名或密码错误,权限不足" << endl;
		}
	}
	~MySystemProxy() {
		if (pSystem != NULL) {
			delete pSystem;
		}
	}
public:
	MySystem* pSystem;
	string mUsername;
	string mPassword;
};

void test01() {
	MySystemProxy* proxy = new MySystemProxy("admin", "admin");
	proxy->run();
}
int main() {
	test01();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值