职责链模式
Posted manual-linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了职责链模式相关的知识,希望对你有一定的参考价值。
一 概念
- 职责链模式,使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。
二 UML图
- Handler 定义一个处理请示的接口
- ConcreteHandler1, ConcreteHandler2 具体处理类,处理它负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。
三 职责链的好处
- 接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可以简化对象的相互连接,他们仅保持一个指向后继者的引用,而不需要保持它所有的候选接收者的引用。
- 可以随时地增加或修改处理一个请求的结构,增强了给对象指派职责的灵活性。
- 不过也应当小心,一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理。
四 C++代码实现
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
class Request
public:
void SetRequestType(string type)
this->m_requestType = type;
string GetRequestType()
return this->m_requestType;
void SetRequestContent(string content)
this->m_requestContent = content;
string GetRequestContent()
return this->m_requestContent;
void SetRequestNumber(int number)
this->m_number = number;
int GetRequestNumber() const
return this->m_number;
private:
string m_requestType; //申请类别
string m_requestContent; //申请内容
int m_number; //申请的数量
;
class Manager
public:
Manager(string name)
this->m_name = name;
//设置管理者的上级
void SetSuperior(Manager* superior)
this->m_superior = superior;
//申请请求
virtual void RequestApplications(Request* request) = 0;
protected:
string m_name;
Manager* m_superior; //管理者的上级
;
//经理
class CommonManager : public Manager
public:
CommonManager(string str)
:Manager(str)
void RequestApplications(Request* request) override
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假" && request->GetRequestNumber() <= 2)
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
else
//其余的请求都需要转到上级
if (this->m_superior != nullptr)
this->m_superior->RequestApplications(request);
;
//总监
class Majordomo : public Manager
public:
Majordomo(string str)
:Manager(str)
void RequestApplications(Request* request) override
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假" && request->GetRequestNumber() <= 5)
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
else
//其余的请求都需要转到上级
if (this->m_superior != nullptr)
this->m_superior->RequestApplications(request);
;
//总经理的权限是全部都需要处理
class GeneralManager : public Manager
public:
GeneralManager(string str)
:Manager(str)
void RequestApplications(Request* request) override
//经理所能有的权限就是可准许下属两天内的假期
if (request->GetRequestType() == "请假")
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
else if(request->GetRequestType() == "加薪" && request->GetRequestNumber() <= 500)
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 被批准" << endl;
else if (request->GetRequestType() == "加薪" && request->GetRequestNumber() > 500)
cout << this->m_name << ' ' << request->GetRequestContent() << " 数量" << request->GetRequestNumber() << " 再说吧" << endl;
;
int main()
CommonManager* jinli = new CommonManager("经理");
Majordomo* zongjian = new Majordomo("总监");
GeneralManager* zongjingli = new GeneralManager("总经理");
//设置上级,完全可以根据实际需要来更改设置
jinli->SetSuperior(zongjian);
zongjian->SetSuperior(zongjingli);
//
Request* request = new Request();
request->SetRequestType("请假");
request->SetRequestContent("小菜请假");
request->SetRequestNumber(1);
jinli->RequestApplications(request);
Request* request2 = new Request();
request2->SetRequestType("请假");
request2->SetRequestContent("小菜请假");
request2->SetRequestNumber(4);
jinli->RequestApplications(request2);
Request* request3 = new Request();
request3->SetRequestType("加薪");
request3->SetRequestContent("小菜加薪");
request3->SetRequestNumber(400);
jinli->RequestApplications(request3);
Request* request4 = new Request();
request4->SetRequestType("加薪");
request4->SetRequestContent("小菜加薪");
request4->SetRequestNumber(1000);
jinli->RequestApplications(request4);
return 0;
以上是关于职责链模式的主要内容,如果未能解决你的问题,请参考以下文章