1、职责链模式的提出
在软件开发过程中,发送者经常发送一个数据请求给特定的接收者对象,让其对请求数据进行处理(一个数据请求只能有一个对象对其处理)。如果发送的每个数据请求指定特定的接收者, 将带来发送者与接收者的紧密解耦合问题。职责链模式可以将请求发送者与接收者解耦。职责链模式的处理流程是:设计一个职责链处理基类,该类具有指向下一个待处理类的变量,通过set接口设置下一个待处理的链对象成员;该类具有遍历处理请求的功能,遍历过程中,只要有一个对象处理了请求,则停止遍历;该基类抽象了2个接口分别为:判断是否处理该类型请求和处理消息的功能接口。其他的具体职责链类继承并实现职责链基类的接口。
2、需求描述
有3个职责功能类,每个职责类都只能对特定的请求数据类型进行处理,现有3个请求类型。设计一个能处理不同数据请求的功能代码,该功能代码具有良好的扩展性。数据请求的类型包括:请求类型和描述信息。
3、功能实现
(1)UML图如下:
(2)代码实现如下:
#include <iostream>
#include <string>
enum class EmType
{EM_TYPE_A,EM_TYPE_B,EM_TYPE_C
};class RequestData
{
private:std::string m_strDesc;EmType m_emType;
public:RequestData(const std::string& desc,EmType type):m_strDesc(std::move(desc)),m_emType(type){};EmType getType()const{return m_emType;};const std::string getDesc()const{return m_strDesc;};
};class ChainHandler
{
protected:ChainHandler* nextChain{nullptr};virtual void processHandler(const RequestData& request)=0;virtual bool IsHandlerType(const RequestData& request)=0;
public:void setNextChain(ChainHandler* next){nextChain = next;};void handle(const RequestData& req){if(IsHandlerType(req)){processHandler(req);}else{if(nextChain != nullptr){nextChain->handle(req);}}}virtual ~ChainHandler(){};
};class HanderA:public ChainHandler
{
public:virtual bool IsHandlerType(const RequestData &request) override{if(request.getType() == EmType::EM_TYPE_A){return true;}else{std::cout << "HanderA cannot process the request:" << request.getDesc() << std::endl;return false;}}virtual void processHandler(const RequestData& request) override{std::cout << "HanderA is processing the request: " << request.getDesc() << std::endl;}
};class HanderB:public ChainHandler
{
public:virtual bool IsHandlerType(const RequestData &request) override{if(request.getType() == EmType::EM_TYPE_B){return true;}else{std::cout << "HanderB cannot process the request:" << request.getDesc() << std::endl;return false;}}virtual void processHandler(const RequestData& request) override{std::cout << "HanderB is processing the request: " << request.getDesc() << std::endl;}
};class HanderC:public ChainHandler
{
public:virtual bool IsHandlerType(const RequestData &request) override{if(request.getType() == EmType::EM_TYPE_C){return true;}else{std::cout << "HanderC cannot process the request:" << request.getDesc() << std::endl;return false;}}virtual void processHandler(const RequestData& request) override{std::cout << "HanderC is processing the request: " << request.getDesc() << std::endl;}
};class Client
{
public:void doWork(){ChainHandler* handlerA = new HanderA();ChainHandler* handlerB = new HanderB();ChainHandler* handlerC = new HanderC();handlerA->setNextChain(handlerB);handlerB->setNextChain(handlerC);RequestData reqA("DataA need to handle",EmType::EM_TYPE_A);handlerA->handle(reqA);std::cout << "\n************** next Request ********************\n" << std::endl;RequestData reqC("DataC need to handle",EmType::EM_TYPE_C);handlerA->handle(reqC);delete handlerA;delete handlerB;delete handlerC;handlerA = nullptr;handlerB = nullptr;handlerC = nullptr;}
};int main()
{Client obj;obj.doWork();return 0;
}
程序运行结果如下: