职责链的特点是:
1、对于请求来说,并不关心被谁处理,从而降低请求和处理模块之间的耦合。
2、各个处理模块构成一个链条,把请求一个环节一个环节的向下传递,每个处理模块对于请求可以选择处理或不处理,向下传递或终止传递。
参见相关文章,说的很好
http://www.cnblogs.com/gaochundong/p/design_pattern_chain_of_responsibility.html
职责链这种模式最常用到的场景之一就是驱动开发了,无论windows还是linux,形式都差不多,都是把自己实现的处理函数添加到处理链条上。
现在就用C++来实现一个职责链。包括3个类,一个是请求类,一个是请求处理类,一个是管理请求处理模块和接受请求的平台。
有兄弟可能会说了,你这个和前面JAVA的实现不一样啊?没有关系,我是考虑到实际开发的情况和个人开发风格。关键的要点是一样的。背后的实现就是一个单链表。
//ChainOfResposibility.h
#ifndef CHAIN_OF_RESPOSIBILITY_H
#define CHAIN_OF_RESPOSIBILITY_H
#include <stddef.h>
class CRequestOfChain
{
public:
enum REQUEST_TYPE
{
REQUEST_TYPE0,
REQUEST_TYPE1,
REQUEST_TYPE2
};
REQUEST_TYPE m_requestType;
};
class CRequestProcess
{
public:
CRequestProcess(void):m_next(NULL){}
virtual ~CRequestProcess(void){}
virtual void Handle(CRequestOfChain & request)=0;
bool SetNext(CRequestProcess & next);
protected:
CRequestProcess(const CRequestProcess &);
CRequestProcess & operator = (const CRequestProcess &);
void SendToNext(CRequestOfChain & request);
CRequestProcess* m_next;
};
class CReiveRequest
{
public:
CReiveRequest(void):m_head(NULL),m_tail(NULL){}
~CReiveRequest(void){}
bool Submit(CRequestOfChain & request);
void AddChain(CRequestProcess & process);
protected:
CReiveRequest(const CReiveRequest &);
CReiveRequest & operator = (const CReiveRequest &);
CRequestProcess* m_head;
CRequestProcess* m_tail;
};
#endif
//ChainOfResposibility.cpp
#include "ChainOfResposibility.h"
bool CRequestProcess::SetNext(CRequestProcess & next)
{
if (NULL != m_next)
return false; //已经有下一个节点了
m_next = &next;
return true;
}
void CRequestProcess::SendToNext(CRequestOfChain & request)
{
m_next->Handle(request);
}
bool CReiveRequest::Submit(CRequestOfChain & request)
{
if (NULL == m_head)
return false;
m_head->Handle(request);
return true;
}
void CReiveRequest::AddChain(CRequestProcess & process)
{
if (NULL == m_head)
{
m_head = &process;
m_tail = &process;
}
else
{
m_tail->SetNext(process);
m_tail = &process;
}
}
//ChainOfResposibilityImpl.h
#ifndef CHAIN_OF_RESPOSIBILITY_IMPL_H
#define CHAIN_OF_RESPOSIBILITY_IMPL_H
#include "ChainOfResposibility.h"
class CProcess1:public CRequestProcess
{
public:
CProcess1(void){}
~CProcess1(void){}
virtual void Handle(CRequestOfChain & request);
};
class CProcess2:public CRequestProcess
{
public:
CProcess2(void){}
~CProcess2(void){}
virtual void Handle(CRequestOfChain & request);
};
class CProcess3:public CRequestProcess
{
public:
CProcess3(void){}
~CProcess3(void){}
virtual void Handle(CRequestOfChain & request);
};
#endif
//ChainOfResposibilityImpl.cpp
#include "ChainOfResposibilityImpl.h"
#include <iostream>
void CProcess1::Handle(CRequestOfChain & request)
{
switch (request.m_requestType)
{
case CRequestOfChain::REQUEST_TYPE0:
std::cout<<"CProcess1:"<<"处理了类型为0的请求,到此为止,其他处理模块不会再收到该请求。"<<std::endl;
break;
case CRequestOfChain::REQUEST_TYPE1:
std::cout<<"CProcess1:"<<"处理了类型为1的请求,处理完后转发给其他处理模块。"<<std::endl;
SendToNext(request);
break;
default:
std::cout<<"CProcess1:"<<"该类型的请求不处理,转发给其他处理模块。"<<std::endl;
SendToNext(request);
}
}
void CProcess2::Handle(CRequestOfChain & request)
{
if (CRequestOfChain::REQUEST_TYPE1 == request.m_requestType)
{
std::cout<<"CProcess2:"<<"处理了类型为1的请求,到此为止,其他处理模块不会再收到该请求。"<<std::endl;
}
else
{
std::cout<<"CProcess2:"<<"该类型的请求不处理,转发给其他处理模块。"<<std::endl;
SendToNext(request);
}
}
void CProcess3::Handle(CRequestOfChain & request)
{
if (CRequestOfChain::REQUEST_TYPE2 == request.m_requestType)
{
std::cout<<"CProcess3:"<<"处理了类型为2的请求,到此为止,其他处理模块不会再收到该请求。"<<std::endl;
}
else
{
std::cout<<"CProcess2:"<<"该类型的请求不处理,转发给其他处理模块。"<<std::endl;
SendToNext(request);
}
}
#include "stdafx.h"
#include "ChainOfResposibilityImpl.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
CRequestOfChain request0;
request0.m_requestType = CRequestOfChain::REQUEST_TYPE0;
CRequestOfChain request1;
request1.m_requestType = CRequestOfChain::REQUEST_TYPE1;
CRequestOfChain request2;
request2.m_requestType = CRequestOfChain::REQUEST_TYPE2;
CProcess1 process1;
CProcess2 process2;
CProcess3 process3;
CReiveRequest platform;
platform.AddChain(process1);
platform.AddChain(process2);
platform.AddChain(process3);
std::cout<<"处理请求0"<<std::endl;
platform.Submit(request0);
std::cout<<std::endl;
std::cout<<"处理请求1"<<std::endl;
platform.Submit(request1);
std::cout<<std::endl;
std::cout<<"处理请求2"<<std::endl;
platform.Submit(request2);
return 0;
}
运行结果如下图
需要注意的是,为了简化代码,这里没有进行加锁之类的同步处理,在多线程环境下有同步问题。