责任链模式

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

    责任链模式是继承 + 委托的运用。在以下代码中,定义一个抽象管理者类,派生经理类,总监类,总经理类。有些决定经理可以做,如果经理不能做,那么调用其委托的总监来做,若总监也不能做,则调用其委托的总经理来做。

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

上代码,亲测有效!
 

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

//抽象管理者
class AbstractManage
{
public:
	virtual void DealWithRequest(string workName, int money) = 0;
	string m_ManageName;
	AbstractManage* m_Manage;
};

//经理
class CommonManage : public AbstractManage
{
public:
	CommonManage(string manageName, AbstractManage* manage)
	{
		m_ManageName = manageName;
		m_Manage = manage;
	}
	void DealWithRequest(string workName, int money)
	{
		if (money < 2000)
		{
			cout << "经理" << this->m_ManageName << "同意" << workName << "加薪" << money << "元" << endl;
			return;
		}
		cout << "经理无法审批,交由总监" << endl;
		m_Manage->DealWithRequest(workName, money);
	}
};


//总监
class MajorManage : public AbstractManage
{
public:
	MajorManage(string manageName, AbstractManage* manage)
	{
		m_ManageName = manageName;
		m_Manage = manage;
	}
	void DealWithRequest(string workName, int money)
	{
		if (money < 5000)
		{
			cout << "总监" << this->m_ManageName << "同意" << workName << "加薪" << money << "元" << endl;
			return;
		}
		cout << "总监无法审批,交由总经理" << endl;
		m_Manage->DealWithRequest(workName, money);
	}
};
//总经理
class GeneralManage : public AbstractManage
{
public:
	GeneralManage(string manageName)
	{
		m_ManageName = manageName;
	}
	void DealWithRequest(string workName, int money)
	{
			cout << "总经理" << this->m_ManageName << "同意" << workName << "加薪" << money << "元" << endl;
			return;
	}
};

int main(void)
{
	GeneralManage* generalManage = new GeneralManage("裘千丈");
	MajorManage* majorManage = new MajorManage("裘千仞", generalManage);
	CommonManage* commonManage = new CommonManage("裘千尺", majorManage);
	//处理请求
	commonManage->DealWithRequest("杨过", 50000);
	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、付费专栏及课程。

余额充值