代理模式实现

    学了C++基本的语法都知道继承可以让子类拥有更多的功能,除了继承还有组合,委托,也能让一个类的功能增加。设计模式,这个设计是设计继承,组合,委托,之间相互叠加的方式,让其符合业务需求。

    代理模式相对简单很多,当然这需要你对委托熟悉。在一个类中,把另一个类的对象指针作为它的数据成员,在访问这个成员前,需要满足一定的条件,很简单,直接看代码。

    这些代码都是在学习这些的过程中码的。。。。。

上代码,亲测有效!
 

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



//提供一种代理控制对其他对象的访问
class MySystem
{
public:
	virtual void run()
	{
		cout << "启动系统..." << endl;
	}
};

class MySystemProxy
{
public:
	MySystemProxy(string userName, string passWard)
	{
		this->m_PassWord = userName;
		this->m_UserName = passWard;
		this->pSystem = new MySystem;
	}
	bool check()
	{
		if (m_UserName == "admin" && m_PassWord == "admin")
		{
			return true;
		}
		return false;
	}
	void run()
	{
		//满足登录条件才能启动系统
		//代理是对真正系统的管理
		if (check())
		{
			cout << "登录成功" << endl;
			this->pSystem->run();
		}
		else
		{
			cout << "用户名或密码错误" << endl;
		}
	}
	~MySystemProxy()
	{
		if (this->pSystem != NULL)
		{
			delete this->pSystem;
		}
	}
	//代理要能完成系统的所有功能
	//因此需要拿到系统的对象指针
	MySystem* pSystem;
	string m_UserName;
	string m_PassWord;
};

//必须要有权限的验证才能启动系统
//提供用户名和密码
//代理就是系统




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

int main(void)
{
	test01();
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

、、、、南山小雨、、、、

分享对你有帮助,打赏一下吧!

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

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

打赏作者

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

抵扣说明:

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

余额充值