C++实现责任链模式
描述
责任链模式,Chain of responsibility,是GoF中的behavior pattern行为模式中的一种,在所有的行为模式中,都设计的准则为:使用对象的组合而不是对象的继承。责任链模式所描述的是:与其从已有的类中继承新的类,不如让你的类指向一个你要使用的已经存在的类。该模式用于解耦发送请求的客户与处理请求的对象,实现方式为持有一系列的handler对象,每个对象都能处理特定的请求,如果某个handler不能处理该请求,它将请求传递给链中的下一个对象。在链的末端,会有一个或多个通用的(generic)handler,用于对请求的默认处理。
该模式为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。
类图:
例子
下面的例子是一个客户服务系统,用于响应用户的不同请求,各个类的说明如下:
- AbstractSupportHandler:抽象基类,用于作为处理请求的接口。如果特定等级的请求该类无法处理,则传递给下一个handler
- ConcreteHandler(TechnicalSupportHandler, BillingSupportHandler, and GeneralSupportHandler):处理请求,或将请求传递给handler链中的下一个
- Client(RequestorClient):发起请求
代码:
AbstractSupportHandler
#ifndef _ABS_SUPPORT_HANDLER_H_ #define _ABS_SUPPORT_HANDLER_H_ #include <string> using std::string; //enum class HANDLEER_ENUM enum HANDLEER_ENUM { TECHNICAL = 0, BILLING = 1, GENERAL =2 }; class AbstractSupportHandler { public: AbstractSupportHandler(); virtual ~AbstractSupportHandler(){} virtual void handleRequest(string message) = 0; //set next handler void setNextHandler(AbstractSupportHandler* handler) { this->m_nextHandler = handler; } //receive request void receiveRequest(int level, string message); protected: int level; AbstractSupportHandler* m_nextHandler; }; #endif
#include "AbstractSupportHandler.h" AbstractSupportHandler::AbstractSupportHandler() { } //receive request void AbstractSupportHandler::receiveRequest(int level, string message) { if (level >= this->level) { this->handleRequest(message); } if (this->m_nextHandler != nullptr) { this->m_nextHandler->receiveRequest(level, message); } }
TechnicalSupportHandler:AbstractSupportHandler的子类
#ifndef _TECHNICAL_SUPPORT_HANDLER_H_ #define _TECHNICAL_SUPPORT_HANDLER_H_ #include "AbstractSupportHandler.h" class TechnicalSupportHandler : public AbstractSupportHandler { public: TechnicalSupportHandler(int level); ~TechnicalSupportHandler(); void handleRequest(string message) override; }; #endif
#include "TechnicalSupportHandler.h" #include <iostream> using namespace std; TechnicalSupportHandler::TechnicalSupportHandler(int level) { this->level = level; } TechnicalSupportHandler::~TechnicalSupportHandler() { } void TechnicalSupportHandler::handleRequest(string message) { cout << "TechnicalSupportHandler: Processing request " + message << endl; }
另外两个类:BillingSupportHandler、GeneralSupportHandler与此类似,省略。
RequestorClient:
#ifndef _REQUESTOR_CLIENT_H_ #define _REQUESTOR_CLIENT_H_ #include "AbstractSupportHandler.h" class RequestorClient { public: RequestorClient(); ~RequestorClient(); static AbstractSupportHandler* getHandlerChain(); }; #endif
#include "RequestorClient.h" #include "TechnicalSupportHandler.h" #include "GeneralSupportHandler.h" #include "BillingSupportHandler.h" RequestorClient::RequestorClient() { } RequestorClient::~RequestorClient() { } AbstractSupportHandler* RequestorClient::getHandlerChain() { AbstractSupportHandler* technicalHandler = new TechnicalSupportHandler(HANDLEER_ENUM::TECHNICAL); AbstractSupportHandler* billingHandler = new BillingSupportHandler(HANDLEER_ENUM::BILLING); AbstractSupportHandler* generalHandler = new GeneralSupportHandler(HANDLEER_ENUM::GENERAL); technicalHandler->setNextHandler(billingHandler); billingHandler->setNextHandler(generalHandler); return technicalHandler; }
main函数用于测试:
#include <iostream> #include <stdlib.h> #include "AbstractSupportHandler.h" #include "RequestorClient.h" using namespace std; int main(int argc, char *argv[]) { AbstractSupportHandler* handler = RequestorClient::getHandlerChain(); handler->receiveRequest(HANDLEER_ENUM::TECHNICAL, "I'm having problem with my internet connectivity"); cout << "==============================" << endl; handler->receiveRequest(HANDLEER_ENUM::BILLING, "Please resend my bill of this month."); cout << "==============================" << endl; handler->receiveRequest(HANDLEER_ENUM::GENERAL, "Please send any other plans for home users."); system("pause"); }
运行结果为::
总结:
责任链模式实现了发起请求的客户与处理请求的handler之间的完全解耦。在该模式中,第一个handler接收客户的请求,它要么处理该请求,要么将该请求传递给链中的下一个handler。在该门模式中,发起请求的client完全不知道哪个handler会处理它的请求,并且最终处理该请求的对象也不知道到底是谁发出的该请求。
参考:
https://springframework.guru/gang-of-four-design-patterns/chain-of-responsibility-pattern/