Overview Of Chain Of Responsibility Design Pattern

Arshad Suraj
Geek Culture
Published in
3 min readJun 2, 2021
Image from google

What is Chain of responsibility design pattern?

The Chain Of Responsibility pattern enables you to loosely couple the senders and receivers of a request. We build a receiver objects chain in this pattern, with each receiver containing a reference to another receiver. This chain receives a request from the client and starts processing it. If first object is unable to handle the request, it is passed to the next receiver, and so on, until the request is fully handled.

During the run time, the objects in the chain will decide who will handle the request and whether it needs to be passed on to the next object in the chain. The Chain of responsibility design pattern is classified as a behavioural pattern.

Things to keep in mind.

  • An abstract parent class with an object reference to its own type must be created.
  • This parent class should provide a setter() method for setting the object created above. We’ll be able to create our object chain as a result of this.
  • All receiver classes should extend from the abstract parent class that was defined above.

Implementation

Class Diagram of the chain of responsibility design pattern

Let’s get a better understanding of the topic by jumping into the code.

Assume we need to develop a program for ATM machine. when a user enters the amount he wishes to withdraw, our program should calculate how many 5000,2000,1000,500 and 100LKR banknotes should be given through the machine. See the codes below,

Code snippet of parent class

Above is the abstract parent class and below are its child handler classes.

Below is the class which’s object is sent as a request,

Below is the main class,

Below is the output for the above code,

Output of the above codes

If you wish to change the workflow, you can achieve it by simply changing the chain.

Assume If the government bans 500LKR banknotes, then our program should calculate 100LKR notes after counting the 1000LKR notes. Right? To do that change the chain as shown below,

noof5000.setMoney(noof2000);
noof2000.setMoney(noof1000);
noof1000.setMoney(noof100);

Output:

Output of the above change

As a result, we can understand that in the Chain of responsibility design pattern sender and receiver of the request are loosely coupled. we can change the workflow order by simply changing the chain (component) without changing the code of particular class.

Advantage of Chain Of Responsibility pattern.

  • It supports loosely coupling.
  • Objects have been simplified. The chain structure does not need to be known by the object.
  • When assigning tasks to objects, it adds flexibility. We can add dynamic responsibility by changing the nodes in the chain.
  • It allows a set of classes, to function as one. Requests generated by one class can be passed to another class via composition.

When to use Chain Of Responsibility pattern?

  • When you wish to separate the sender and receiver of a request.
  • When multiple objects need to handle the request.
  • When you don’t want to declare each handler in your code directly.

Keep Learning ❤️

--

--