> Design Patterns > Chain of Responsibility Design Pattern

Chain of Responsibility Design Pattern

Chain of responsibility design pattern is used to provide a chain of objects to handle the request.

Intent

  • 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.

The intent of chain of responsibility design pattern is to chain the receiving objects to handle the request one by one till it gets handled.

Problem

  • Sender object does not know which object is going to handle the request.

There may be a situation where the sender does not know which specific object has to handle its request.

Solution

  • Have multiple objects to handle the request of sender object.

So we can have multiple objects in chain and handle the request one by one till the request is handled.

Where chain of responsibility design pattern is applicable?

  • There may be multiple object which can handle the request.
  • The sender object does not know which object can handle its request.

Chain of Responsibility Design Pattern UML Structure

Chain of Responsibility Design Pattern UML Structure

Participant classes of chain of responsibility design pattern

  • Handler class provides interface HandleRequest() to client. It also has member to keep the successor of handler object and method MakeSuccessor() to set the successor.
  • The concrete classes ConcreteHandler1 and ConcreteHandler2 implement the method HandleRequest().

How they work together?

  • Client requests to handle the request, ConcreteHandler handles the request if it can, otherwise it passes it to another object in chain through successor information. So all the objects in chain one by one try to handle the request in same way till last object.
  • So we can see it decouples sender and requester. Also it provides flexibility to add further responsibility in handler. There may be cases when the request cannot be handled at all.

Chain of Responsibility Design Pattern Example

Here is the chain of responsibility design pattern example.

Network Management Explorer has Networks, node like system, database, printer etc. Some command like Configure should be executed based on what is selected.

Implementation Code

C++ Chain of Responsibility Pattern in C++ Chain of Responsibility Pattern C++ Example
C# Chain of Responsibility Pattern in C# Chain of Responsibility Pattern C# Example
Java Chain of Responsibility Pattern in Java Chain of Responsibility Pattern Java Example
Python Chain of Responsibility Pattern in Python Chain of Responsibility Pattern Python Example
JavaScript Chain of Responsibility Pattern in JavaScript Chain of Responsibility Pattern JavaScript Example
PHP Chain of Responsibility Pattern in PHP Chain of Responsibility Pattern PHP Example
Ruby Chain of Responsibility Pattern in Ruby Chain of Responsibility Pattern Ruby Example
Swift Chain of Responsibility Pattern in Swift Chain of Responsibility Pattern Swift Example
Objective-C Chain of Responsibility Pattern in Objective-C Chain of Responsibility Pattern Objective-C Example


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article Chain of Responsibility Design Pattern is from his Design Patterns course.