zoukankan      html  css  js  c++  java
  • 第二十四章-职责链模式

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

    职责链的好处是当客户提交一个请求时,请求是沿链传递直至有一个ConcreteHandler对象负责处理它。使得接受者和发送者都没有对方的明确消息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持他所有的候选接受者的引用。不过也要当心,一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理。

    图片

    加薪代码

    图片

    #include <iostream>
    #include<vector>
    #include<string>
    #include<ctime>
    
    using namespace std;
    
    class Request
    {
    private:
    	string requestType;
    	string requestContent;
    	int number;
    public:
    	string getRequestType() { return requestType; }
    	void setSequestType(string requestType_t) { requestType = requestType_t; }
    
    	string getRequestContent() { return requestContent; }
    	void setRequestContent(string requestContent_t) { requestContent = requestContent_t; }
    
    	int getNumber() { return number; }
    	void setNumber(int number_t) { number = number_t; }
    };
    
    class Manager
    {
    protected:
    	string name;
    	Manager* superior;
    
    public:
    	Manager(string name_t)
    	{
    		name = name_t;
    	}
    
    	void SetSuperior(Manager* superior_t)
    	{
    		superior = superior_t;
    	}
    
    	virtual void RequestApplications(Request* request) = 0;
    };
    
    class CommonManager :public Manager
    {
    public:
    	CommonManager(string name_t)
    		:Manager(name_t)
    	{ }
    
    	void RequestApplications(Request* request)
    	{
    		if (request->getRequestType() == "请假" && request->getNumber() <= 2)
    		{
    			cout << request->getRequestType() << "数量" << request->getNumber() << " 被批准" << endl;
    		}
    		else
    		{
    			if (superior != nullptr)
    				superior->RequestApplications(request);
    		}
    	}
    };
    
    class Majordomo :public Manager
    {
    public:
    	Majordomo(string name_t)
    		:Manager(name_t)
    	{ }
    
    	void RequestApplications(Request* request)
    	{
    		if (request->getRequestType() == "请假" && request->getNumber() <= 5)
    		{
    			cout << request->getRequestType() << "数量" << request->getNumber() << " 被批准" << endl;
    		}
    		else
    		{
    			if (superior != nullptr)
    				superior->RequestApplications(request);
    		}
    	}
    };
    
    class GeneralManager :public Manager
    {
    public:
    	GeneralManager(string name_t)
    		:Manager(name_t)
    	{ }
    
    	void RequestApplications(Request* request)
    	{
    		if (request->getRequestType() == "请假")
    		{
    			cout << request->getRequestType() << "数量" << request->getNumber() << " 被批准" << endl;
    		}
    		else if (request->getRequestType() == "加薪" && request->getNumber() <= 500)
    		{
    			cout << request->getRequestType() << "数量" << request->getNumber() << " 被批准" << endl;
    		}
    		else if (request->getRequestType() == "加薪" && request->getNumber() > 500)
    		{
    			cout << request->getRequestType() << "数量" << request->getNumber() << " 再说吧" << endl;
    		}
    	}
    };
    
    
    int main()
    {
    	CommonManager* jingli = new CommonManager("经理");
    	Majordomo* zongjian = new Majordomo("总监");
    	GeneralManager* zongjingli = new GeneralManager("总经理");
    	jingli->SetSuperior(zongjian);
    	zongjian->SetSuperior(zongjingli);
    
    	Request* request = new Request();
    	request->setSequestType("请假");
    	request->setRequestContent("小菜请假");
    	request->setNumber(1);
    	jingli->RequestApplications(request);
    
    
    	system("pause");
    	return 0;
    }
    
  • 相关阅读:
    小例子-使用JS/JQ获取a标签的href网址
    R语言随手记-线性回归模型诊断
    R语言随手记-数据处理
    正选择分析-PAML discussion group
    ggtree-基本函数使用
    PhastCons
    R语言随手记-批量读取和循环处理多个数据文件
    变异位点有害性软件评估
    ggplot画图-分页and排序
    R画图-标题和坐标轴篇
  • 原文地址:https://www.cnblogs.com/wfcg165/p/12052794.html
Copyright © 2011-2022 走看看