Chain of responsibility pattern

 Introduction:

Chain of responsibility is a behavioral design pattern. This pattern pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.
  • Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 
  • Launch-and-leave requests with a single processing pipeline that contains many possible handlers.
  • An object-oriented linked list with recursive traversal.
  • Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain.
  • Make sure there exists a "safety net" to "catch" any requests which go unhandled.
  • Do not use Chain of Responsibility when each request is only handled by one handler, or, when the client object knows which service object should handle the request.
  • The derived classes know how to satisfy Client requests. If the "current" object is not available or sufficient, then it delegates to the base class, which delegates to the "next" object, and the circle of life continues.

UML diagram:

























When to use, 

There is a potentially variable number of "handler" or "processing element" or "node" objects, and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.

How to implement, 

  • Define interface Handler class which contains pure virtual method to set next handler and handle client request.
  • Define baseHandler class derived from handler interface, and provide the implementation setNextHandler and handle() methods. Create private field for Handler object.
  • Create concrete handler classes, and provide the actual implementation of the "handle" method. If the class can't handle the request, pass the request to the base class. 
  • In the client side, create the concrete handler objects , and set the next handler. 
  • Pass the request to the base class.

Implementation:

Sourcecode: https://github.com/krishnaKSA/design_patterns_cplusplus/blob/main/ChainOfResponsibility/ChainOfResponsibility.hpp

Class Diagram:






















Handler Interface:

    //Handler Interface
    class IHandler
    {
        public:
        virtual IHandler* setNextHandler(IHandler* nextHandler) = 0;
        virtual std::string handle(string request) = 0;
    };

Base Handler:

    //Base handler
    class BaseHandler: public IHandler
    {
        private:
        IHandler* nextHandler;

        public:
        BaseHandler():nextHandler(nullptr)
        {

        }
        IHandler* setNextHandler(IHandler* nextHandler) override
        {
            this->nextHandler = nextHandler;
            return nextHandler;
        }

        std::string handle(string request) override
        {
            if(nullptr != this->nextHandler)
            {
                return this->nextHandler->handle(request);
            }

            return {};
        }
    };

Concrete Handler classes:

    class IntHandler: public BaseHandler
    {

        public:
        std::string handle(string request) override
        {
            if("int" == request)
            {
                return "IntHandler::request processed";
            }
            return BaseHandler::handle(request);
        }
    };
    class FloatHandler: public BaseHandler
    {
        public:
        std::string handle(string request) override
        {
            if("float" == request)
            {
                return "FloatHandler::request processed";
            }
            return BaseHandler::handle(request);
        }
    };

    class StringHandler: public BaseHandler
    {
        public:
        std::string handle(string request) override
        {
            if("string" == request)
            {
                return "StringHandler::request processed";
            }
            return BaseHandler::handle(request);
        }
    };

Client code:

    void testClientCode()
    {
        BaseHandler* baseHandler = new BaseHandler;
        IntHandler* intHandler = new IntHandler;
        FloatHandler* floatHandler = new FloatHandler;
        StringHandler* stringHandler = new StringHandler;

        baseHandler->setNextHandler(intHandler)->setNextHandler(floatHandler)->setNextHandler(stringHandler);

        cout << baseHandler->handle("string") << endl;
        cout << baseHandler->handle("float") << endl;
        cout << baseHandler->handle("int") << endl;
    }