Decoupling is one of the prominent mantras in software engineering.
Chain of responsibility helps to decouple sender of a request and receiver of the request with some trade-offs.
Chain of responsibility is a design pattern where a sender sends a request to a chain of objects, where the objects in the chain decide themselves who to honor the request. If an object in the chain decides not to serve the request, it forwards the request to the next object in the chain.
In a chain of objects, the responsibility of deciding who to serve the request is left to the objects participating in the chains.
èIt is similar to ‘passing the question in a quiz scenario’. When the quiz master asks a question to a person, if he doesn’t knows the answer, he passes the question to next person and so on. When one person answers the question, the passing flow stops. Sometimes, the passing might reach the last person and still nobody gives the answer.
Chain of Responsibility Pattern Examples
ATM dispense machine.
Usage of Chain of Responsibility Pattern in JDK