职责链模式,使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连城一条链,并沿着这条链传递该请求,知道有一个对象处理它为止。
职责链模式的好处
当客户提交一个请求时,请求是沿链传递直至有一个ConcreteHandler对象负责处理它。接受者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需要保持一个指向其后继者的引用,而不需要保持它所有的候选接受者的引用。可以随时地增加或修改一个请求的结构。增强了给对象指派职责的灵活性。一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理。
#include <iostream>
using namespace std;
class Request //职员请求类
{
string requestType; //请求类型 请假、加薪
string requestContent; //请假条具体内容
int number; //请假天数、加薪金额
public:
void setrequestType(string reT){requestType = reT;} //设置requestType
string getrequestType(){return requestType;} //获取requestType
void setrequestContent(string reC){requestContent = reC;} //设置requestContent
string getrequestContent(){return requestContent;} //获取requestContent
void setnumber(int n){number = n;} //设置number
int getnumber(){return number;} //获取number
};
class Manager //管理人员抽象类
{
public:
string m_name; //管理人员名字
Manager* m_superior; //高一级管理层
public:
Manager(string name):m_name(name){}; //传入名字
void setsuperior(Manager* superior) //设置上一级管理层
{
m_superior = superior;
}
virtual void RequestApplication(Request* request) = 0; //处理请求虚函数
};
class CommendManager : public Manager //经理类
{
public:
CommendManager(string name):Manager(name){}; //传入名字
void RequestApplication(Request* request) //处理请求
{
if(request->getrequestType() == "请假" && request->getnumber() <= 2)
{
cout<<this->m_name<<":"<<request->getrequestContent()<<" 天数 "<<request->getnumber()<<" 被批准"<<endl;
}
else
{
if(m_superior != NULL) //若有上级 转到上级处理
{
m_superior->RequestApplication(request);
}
}
}
};
class Majordomo : public Manager //总监类
{
public:
Majordomo(string name):Manager(name){}; //传入名字
void RequestApplication(Request* request) //处理请求
{
if(request->getrequestType() == "请假" && request->getnumber() <= 5)
{
cout<<this->m_name<<":"<<request->getrequestContent()<<" 天数 "<<request->getnumber()<<" 被批准"<<endl;
}
else //若有上级 转到上级处理
{
if(m_superior != NULL)
{
m_superior->RequestApplication(request);
}
}
}
};
class GeneralManager : public Manager //总经理类
{
public:
GeneralManager(string name):Manager(name){}; //传入名字
void RequestApplication(Request* request) //处理请求
{
if(request->getrequestType() == "请假")
{
cout<<this->m_name<<":"<<request->getrequestContent()<<" 天数: "<<request->getnumber()<<" 被批准"<<endl;
}
else if(request->getrequestType() == "加薪" && request->getnumber() <= 500)
{
cout<<this->m_name<<":"<<request->getrequestContent()<<" 金额 "<<request->getnumber()<<" 被批准"<<endl;
}
else if(request->getrequestType() == "加薪" && request->getnumber() > 500)
{
cout<<this->m_name<<":"<<request->getrequestContent()<<" 金额 "<<request->getnumber()<<" 再说吧"<<endl;
}
}
};
int main()
{
/*实例化经理、总监、总经理并设置上下级关系*/
Manager* smallboss = new CommendManager("尼古拉斯");
Manager* bigboss = new Majordomo("富贵");
Manager* superboss = new GeneralManager("吉祥");
smallboss->setsuperior(bigboss);
bigboss->setsuperior(superboss);
/*设置请求*/
Request* request = new Request();
request->setrequestType("请假");
request->setrequestContent("小张请假");
request->setnumber(1);
smallboss->RequestApplication(request);
/*设置请求*/
request->setrequestType("请假");
request->setrequestContent("小张请假");
request->setnumber(4);
smallboss->RequestApplication(request);
/*设置请求*/
request->setrequestType("加薪");
request->setrequestContent("小李请求加薪");
request->setnumber(450);
smallboss->RequestApplication(request);
/*设置请求*/
request->setrequestType("加薪");
request->setrequestContent("小王请求加薪");
request->setnumber(600);
smallboss->RequestApplication(request);
return 0;
}
[hjf@hjf 设计模式]$ ./a.out
尼古拉斯:小张请假 天数 1 被批准
富贵:小张请假 天数 4 被批准
吉祥:小李请求加薪 金额 450 被批准
吉祥:小王请求加薪 金额 600 再说吧