设计模式——职责链模式(C++实现)

设计模式——职责链模式(C++实现)

 

 #include <iostream>
#include <string> using namespace std; class Handler
{
public:
Handler(Handler* pstHandler): m_pstHandler(pstHandler)
{ } virtual void HandleRequest(int request) = ; protected:
Handler* m_pstHandler;
}; class ConcreteHandler1: public Handler
{
public:
ConcreteHandler1(Handler* pstHandler): Handler(pstHandler)
{ } virtual void HandleRequest(int request)
{
if (request < )
{
cout<< "组长:批假"<< request<< "天"<< endl;
}
else
{
m_pstHandler->HandleRequest(request);
}
}
}; class ConcreteHandler2: public Handler
{
public:
ConcreteHandler2(Handler* pstHandler): Handler(pstHandler)
{ } virtual void HandleRequest(int request)
{
if (request < )
{
cout<< "总监:批假"<< request<< "天"<< endl;
}
else
{
m_pstHandler->HandleRequest(request);
}
}
}; class ConcreteHandler3: public Handler
{
public:
ConcreteHandler3(Handler* pstHandler): Handler(pstHandler)
{ } virtual void HandleRequest(int request)
{
if (request < )
{
cout<< "经理:批假"<< request<< "天"<< endl;
}
else
{
cout<< "经理:"<< request<< "天,时间太长,超过公司规定"<< endl;
}
}
}; int main(int argc, char* argv[])
{
Handler* jingli = new ConcreteHandler3(NULL);
Handler* zongjian = new ConcreteHandler2(jingli);
Handler* zuzhang = new ConcreteHandler1(zongjian); zuzhang->HandleRequest();
zuzhang->HandleRequest();
zuzhang->HandleRequest();
zuzhang->HandleRequest(); delete jingli;
delete zongjian;
delete zuzhang; return ;
}
//////////////////////////////////
[root@ ~/learn_code/design_pattern/20_chain]$ ./chain
总监:批假12天
经理:批假15天
组长:批假5天
经理:31天,时间太长,超过公司规定
上一篇:Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 上


下一篇:JAVA-JSP内置对象之request获得封装所有参数值的Map