顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。 在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。
职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。
在处理消息的时候以过滤很多道。
拦截的类都实现统一接口。
Handler 里面聚合它自己,在 HandlerRequest 里判断是否合适,如果没达到条件则向下传递,向谁传递之前 set 进去。
在 JAVA WEB 中遇到很多应用。
我们创建抽象类 AbstractLogger,带有详细的日志记录级别。然后我们创建三种类型的记录器,都扩展了 AbstractLogger。每个记录器消息的级别是否属于自己的级别,如果是则相应地打印出来,否则将不打印并把消息传给下一个记录器。
#include "pch.h" #include <stdlib.h> #include <iostream> #include <vector> #include <unordered_map> class AbstractLogger { public: enum { INFO = 1, DEBUG = 2, ERROR = 3 }; void setNextLogger(AbstractLogger* nextLogger) { this->nextLogger = nextLogger; } void logMessage(int level, std::string message) { if (this->level <= level) { Write(message); } else { if (nextLogger) { nextLogger->logMessage(level, message); } } } protected: virtual void Write(std::string message) = 0; int level; AbstractLogger* nextLogger; //责任链中的下一个元素 }; class ConsoleLogger : public AbstractLogger { public: ConsoleLogger(int level) { this->level = level; } protected: virtual void Write(std::string message) { std::cout << "Standard Console::Logger: " << message.c_str() << std::endl; } }; class ErrorLogger : public AbstractLogger { public: ErrorLogger(int level) { this->level = level; } protected: virtual void Write(std::string message) { std::cout << "Error Console::Logger: " << message.c_str() << std::endl; } }; class FileLogger : public AbstractLogger { public: FileLogger(int level) { this->level = level; } protected: virtual void Write(std::string message) { std::cout << "File::Logger: " << message.c_str() << std::endl; } }; int main() { AbstractLogger* errorLogger = new ErrorLogger(AbstractLogger::ERROR); AbstractLogger* fileLogger = new FileLogger(AbstractLogger::DEBUG); AbstractLogger* consoleLogger = new ConsoleLogger(AbstractLogger::INFO); errorLogger->setNextLogger(fileLogger); fileLogger->setNextLogger(consoleLogger); AbstractLogger* loggerChain = errorLogger; loggerChain->logMessage(AbstractLogger::INFO, "This is an information."); loggerChain->logMessage(AbstractLogger::DEBUG, "This is a debug level information."); loggerChain->logMessage(AbstractLogger::ERROR, "This is an error information."); } >>> Standard Console::Logger: This is an information. File::Logger: This is a debug level information. Error Console::Logger: This is an error information