CHAIN OF RESPONSIBILITY

Hansini Rupasinghe
7 min readMay 26, 2021

What is Chain of Responsibility?

Chain of Responsibility is one of the behavioral design patterns that creates a chain of receiver objects upon a request.

❉ It decouples the sender of the request and the receiver in accordance with the request type.

❉ Once a request is received, each handler decides whether to undertake the request or to pass it into another object in the chain to process.

❉ One receiver has the reference to another receiver.

❉ Receiver can perform its job without knowing who sent the message, and also the sender can too perform its job without knowing who is going to receive results.

❉ In real life, there are many situations where we can use this design pattern to overcome the issues easily. For an example, assume you have to control menu based on the user level. The user levels are as follows.

➣ CEO

➣ Director

➣ Manager

➣ Executive

They have multiple levels of permission. In this case, we can use chain of responsibility design pattern to implement this use case.

❉ In Java API, Logger can be taken as the best example for Chain of Responsibilities. Servlet Filters in Java can be known as another real world example.

❉If you take log levels;

➢ Debug

➢ Warning

➢ Severe

➢ Critical

These levels depend on implementation.

Let us implement this.

OUTPUT

✦ If you set log level into INFO, it will not print anything below infro, but everything above INFO.

If we change the set Level to “WARNING” in Line 9;

✦ If you set log level into WARNING, it will not print anything below infro, but everything above WARNING.

OUTPUT

✦ Therefore, it goes in a hierarchical level.

✦ If you do not find a handler for the particular level, it just ignores that.

UML of Chain of Responsibility Design Pattern

❉ You can change the order of the receivers and senders, but without affecting the core implementation of the program.

Ref:https://dzone.com/articles/using-chain-of-responsibility-design-pattern-in-ja

Client — This is the Originator of request.

Sender — A request will be sent to the chain by the sender.

Receiver — These objects do not have any order and used in any processing order.

Concrete handlers — This will handle the request or pass it on to the next handler in some sequential order.

Usage of Chain of Responsibility Design Pattern

❉ Chain of Responsibility design pattern can be used in following situations when;

  • Multiple objects can handle a request and the handler is not known.
  • You need to decouple sender and receiver of a particular request.
  • Defining the multiple objects that are capable of handling a request dynamically at run time.
  • Specifying handlers explicitly in your program is not needed.
  • The sender is not aware of the receiver and the receiver is not aware of the sender.

Implementation Principles of Chain of Responsibility Design Pattern

📝 There is a separate implementation for each processor in the chain in order to process a command.

📝 One processor must contain reference to the next processor.

📝 Must be attentive of the dropped commands as one processor affects another.

📝 Recursive cycles in processors should not be formed at any cost.

📝 A provided command will be handled by only one processor in the chain.

Real World Example for Chain of Responsibility Design Pattern

📜 “Digital Nexus” is an Information Technology related company where there are 500+ employees. There are different allowances provided by the company based on the employees’ salaries. Employees are categorized into different levels based on the basic salary. Different allowances are added to the salary based on the employee level.

If basic salary range is;

💸 Level 5 : basic salary ≤ 35000 ➜ Medical Allowance (20%)

💸 Level 4: 35000 < basic salary ≤ 55000 ➜ Medical Allowance (20%) + Conveyance Allowance (25%)

💸 Level 3: 55000 < basic salary ≤ 75000 ➜ Medical Allowance (20%) + Conveyance Allowance (25%) + House Rent Allowance (30%)

💸 Level 2: 75000 < basic salary ≤ 95000 ➜ Medical Allowance (20%) + Conveyance Allowance (25%) + House Rent Allowance (30%) + Leave Travel Allowance (20%)

💸 Level 1: basic salary > 95000 ➜ Medical Allowance (20%) + Conveyance Allowance (25%) + House Rent Allowance (30%) + Leave Travel Allowance (20%) + Entertainment Allowance (5%)

Let us see how to implement this program.

💫 AllowanceHandler Class

🔖 If needed, anyone can use an interface too.

💫 Salary Class

🔖 We do not have any other setters here.

💫 Level5 Class

🔖 If the basic salary of an employee is less than or equal to 35000, they are eligible for a Medical Allowance. (That is 20% of their basic salary)

Ex: If an employee’s salary is Rs. 20000, they will get a medical allowance of Rs. 4000. And totally the employee gets an amount of;

Rs. 20000 + Rs. 4000 = Rs. 24000/=

💫 Level4 Class

🔖 If the basic salary of an employee is greater than 3500 and less than or equal to 55000, they are eligible for a Medical Allowance( 20% of their basic salary ) and a Conveyance Allowance (25% of their basic salary).

Ex: If an employee’s salary is Rs. 45000, they will get a medical allowance of Rs. 9000, and a conveyance allowance of Rs. 11250. And totally the employee gets an amount of;

Rs. 9000 + Rs. 11 250 + Rs. 45000 = Rs. 65250/=

💫 Level3 Class

🔖 If the basic salary of an employee is greater than 55000 and less than or equal to 75000, they are eligible for a Medical Allowance( 20% of their basic salary ) , a Conveyance Allowance (25% of their basic salary) and a House Rent Allowance (30% of their basic salary).

Ex: If an employee’s salary is Rs. 63500, they will get a medical allowance of Rs. 12700, a conveyance allowance of Rs. 15875 and a House Rent Allowance of Rs. 19050. And totally the employee gets an amount of;

Rs. 12700 + Rs. 15875 + Rs. 19050 + Rs. 63500= Rs. 111125/=

💫 Level2 Class

🔖 If the basic salary of an employee is greater than 75000 and less than or equal to 95000, they are eligible for a Medical Allowance( 20% of their basic salary ) , a Conveyance Allowance (25% of their basic salary), a House Rent Allowance (30% of their basic salary) and a Leave Travel Allowance (20% of their basic salary).

Ex: If an employee’s salary is Rs. 85000, they will get a medical allowance of Rs. 17000, a conveyance allowance of Rs. 21250 , House Rent Allowance of Rs. 25500 and a Leave Travel Allowance of Rs. 17000 . And totally the employee gets an amount of;

Rs. 17000 + Rs. 21250 + Rs. 25500 + Rs. 17000 + Rs. 85000 = Rs. 165750/=

💫 Level1 Class

🔖If the basic salary of an employee is greater than 95000 , they are eligible for all the above mentioned allowances along with an Entertainment allowance(5% of their basic salary).

Ex: If an employee’s salary is Rs. 100000, they will get a medical allowance of Rs. 20000, a conveyance allowance of Rs. 25000 , House Rent Allowance of Rs. 30000, a Leave Travel Allowance of Rs. 20000 and an Entertainment allowance of Rs. 5000 . And totally the employee gets an amount of;

Rs. 20000 + Rs. 25000 + Rs. 30000 + Rs. 20000 + Rs. 5000 + Rs. 100000= Rs. 200000/=

💫 Application Class

🔖 Let us create multiple Salaries and check if the program works properly.

🔖 Whatever you calculate in this program is not affected on any other.

💫 Allowance Class

🔖 This class does not have any implemented logic and this is used as the beginning of the chain.

OUTPUT

Advantages of Chain of Responsibility Design Pattern

♻️ Reduces coupling degree and encourages loose coupling.

♻️ Increase the flexibility of object assigned responsibilities.

♻️ Objects are simplified since it is not aware of the structure of the chain.

♻️ Permits multiple classes to act as one.

♻️ Helps in choosing a processing strategy at the time of processing.

Disadvantages of Chain of Responsibility Design Pattern

♨️ It may create performance issues if deep stack traces create.

♨️ Difficult to debug and characteristics of operation will be hard to observe.

♨️ Maintenance would be hard if codes get duplicated across processors.

♨️ The chain can be broken easily;

↪︎ the command will be dropped if one processor could not call the next processor successfully.

↪︎ A cycle will occur if the processor calls an incorrect processor.

References

--

--