Importance of Chain of Responsibility Design Pattern

Issath Sesni
3 min readMay 29, 2021

--

Image by refactoring.guru

What is Chain of Responsibility Design Pattern and Why?

This is a widely used behavioral design pattern. Sender sends a request to a chain of objects to process them. Then the object in the chain will decide themselves that who will be processing the request and If one object cannot handle the request then it passes the same to the next object along the chain until an object handles it. Mainly It is used to achieve loosely coupling in the software engineering field. It means avoid coupling the sender to its receiver by giving multiple objects a chance to handle the request. So that each receiver(object) contains the reference of another receiver.

Workflow of Chain of Responsibility Design pattern

Let’s understand some words that are used in this pattern.

Handler : It is an interface or abstract class. It is the main receiver that receives the request and dispatches the request to chain of handlers. It has a reference of only first handler in the chain and does not know about rest of the handlers.

Concrete handlers : They are the actual handlers of the request that chained in some sequential order.

We have to ensure below principles when implementing the chain of responsibility design pattern

  • Every receiver has its implementation for processing a command.
  • Every receiver should have reference to the next receiver.
  • Each receiver is responsible for delegating to the next receiver so beware of dropped commands.
  • Receivers should not form a recursive cycle.
  • Only one receiver handles a given command at a time.

When to use

  • When multiple objects can handle a request and determined at runtime.
  • When you want to decouple the sender and the receiver of a request.
  • When you don’t want to specify handlers explicitly in your code.

Real World Example

Servlet Filters in java :- It allows multiple filters to process an HTTP request, Calling to the Customer care center to fix the problem, Money withdrawal from ATM.

Advantages

  • Decoupling the sender and the receiver of a command
  • Picking a processing strategy at processing-time.
  • It adds flexibility while assigning the duties to objects. And changing the members within the chain or change their order, that allows dynamic adding or deleting duties.
  • It allows a set of classes to act as one; and events produced in one class can be sent to other handler classes with the help of composition.
  • Simplified object, that means the object does not know the chain structure.
  • It processes the request in a convenient way.

Drawbacks

  • If an object fails to call the next objects, then the command will get dropped.
  • If an object calls the wrong object, then it will lead to a cycle.
  • It can create deep stack traces, which can affect performance.
  • It can lead to duplicate code across receivers so maintenance should be increased.
  • Can’t guarantee whether the receiver will receives the request.
  • It may not be easy to observe the characteristics of operation, due to debug.

Note : Don’t use Chain of Responsibility when each request is only handled by one handler, or when the sender knows which receiver should handle the request.

Happy Learning.😊

--

--

Issath Sesni
Issath Sesni

No responses yet