职责链模式

理论

职责链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

职责链模式的优点:

  1. 当客户提交一个请求时,请求是沿着链传递直至有一个 ConcreteHandler 对象负责处理它。使得接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需要保持它所有的候选接收者的引用。大大降低了耦合度。

  2. 可以随时增加或修改一个请求的结构,增强了给对象指派职责的灵活性。

实例

假设公司中有经理、总监、总经理等职务。模式员工请假或请求加薪的场景:小假经理批,大假总经理批;小薪直接批准,大薪再商量

UML类图

代码实现

#include <iostream>
using namespace std;

//申请
class Request {
public:
	string requestType;      //申请类别
	string requestContent;	 //申请内容
	int number;				 //申请数量
};

//管理者
class Manager {
public:
	Manager(string name) {
		this->name = name;
	}
	//设置管理者的上级
	void SetSuperior(Manager* superior) {
		this->superior = superior;
	}
	//申请请求
	virtual void RequestApplications(Request& request) {}

protected:
	string name;
	Manager* superior;  //管理者的上级
};

//经理
class CommonManager :public Manager {
public:
	CommonManager(string _name) :Manager(_name) {}
	void RequestApplications(Request& request) {
		if (request.requestType == "请假" && request.number <= 2) {
			cout << name << ": " << request.requestContent << " 数量" << request.number << " 被批准" << endl;
		}
		else {
			if (superior != nullptr) {
				superior->RequestApplications(request);
			}
		}
	}
};

//总监
class Majordomo :public Manager {
public:
	Majordomo(string _name) :Manager(_name) {}
	void RequestApplications(Request& request) {
		if (request.requestType == "请假" && request.number <= 5) {
			cout << name << ": " << request.requestContent << " 数量" << request.number << " 被批准" << endl;
		}
		else {
			if (superior != nullptr) {
				superior->RequestApplications(request);
			}
		}
	}
};

//总经理
class GeneralManager :public Manager {
public:
	GeneralManager(string _name) :Manager(_name) {}
	void RequestApplications(Request& request) {
		if (request.requestType == "请假") {
			cout << name << ": " << request.requestContent << " 数量" << request.number << " 被批准" << endl;
		}
		else if (request.requestType == "加薪" && request.number <= 500) {
			cout << name << ": " << request.requestContent << " 数量" << request.number << " 被批准" << endl;
		}
		else if (request.requestType == "加薪" && request.number > 500) {
			cout << name << ": " << request.requestContent << " 数量" << request.number << " 再议" << endl;
		}
	}
};

int main()
{
	CommonManager jingli("经理");
	Majordomo zongjian("总监");
	GeneralManager zong("总经理");
	jingli.SetSuperior(&zongjian);
	zongjian.SetSuperior(&zong);

	Request request1;
	request1.requestType = "请假";
	request1.requestContent = "小菜请假";
	request1.number = 1;
	jingli.RequestApplications(request1);

	Request request2;
	request2.requestType = "请假";
	request2.requestContent = "小菜请假";
	request2.number = 4;
	jingli.RequestApplications(request2);

	Request request3;
	request3.requestType = "加薪";
	request3.requestContent = "小菜请求加薪";
	request3.number = 500;
	jingli.RequestApplications(request3);

	Request request4;
	request4.requestType = "加薪";
	request4.requestContent = "小菜请求加薪";
	request4.number = 1000;
	jingli.RequestApplications(request4);

	return 0;
}

运行结果:

 

posted @ 2022-09-22 15:01  KYZH  阅读(82)  评论(0编辑  收藏  举报