An Introduction to Chain of Responsibility Design Pattern
The chain of responsibility pattern can be applied when we need to apply the loosely-coupling concept in our work. This is categorized under the behavioral pattern.
Let’s understand the way of applying this pattern using a real-world example.
Assume, a certain company calculates the salary of each employee for a specific month as, if the working hours of a certain employee lie between 180 hours and 200 hours, 30% of the base is added to the base salary. If the employee’s working hours are between 200 hours and 220 hours, an additional amount from the project is added to the previous and the working hours exceed more than 220 hours, again, another amount is added to the previous.
To implement this, first, I have created the EmployeeSalary class by extending Employee and added some parameters to it. Refer to the following codes.
Next, I have created a class called EmployeeSalaryHandler by implementing the SalaryAdd interface and declared a variable called successor to hold the instance of the next node of this class. Apart from that, I have created another 3 classes called Level1, Level2, and Level3 by extending the EmployeeSalaryHandler class and implementing the incomplete method addSalary() inherited from the EmployeeSalaryHandler class. Refer to the following codes.
The calculation of the salary is done by calling the addSalary(employeeSalary) inside the Level1, Level2, and Level3 classes. Here, the calculation is started at Level1 and if the instance of EmployeeSalary (hours) does not satisfy the condition, next, it points to the successor of the Level1 (Level2) while adding 30% to the base salary. Level2 continues the same until the instance satisfies (hours) the condition. But before doing all these things, we have to set the chain at the beginning. Because of that, I have created a class called BindChain to create this chain. Refer to the following code.
Here, I have created the chain between the 3 levels and set level1 as the initial.
Finally, I implemented the main method inside the MainApplication class. Refer to the following code.
Here, I have implemented 4 instances of EmployeeSalary and called the addSalary(employeeSalary ) method by using level1 as the initial. But, you can change the initial and the order of successors by simply changing the levels inside the BindChain class.
The following defines the output.
Employee Salary Sheet: 0001
Employee Name: Hasini
Employee Position: ASE
Total Hours Worked: 200
Base Salary: 5000.0
Addition: 1500.0
_____________________________
Total Salary: 6500.0
_____________________________
_____________________________Employee Salary Sheet: 0002
Employee Name: Hasini
Employee Position: SE
Total Hours Worked: 250
Base Salary: 6000.0
Addition: 4800.0
_____________________________
Total Salary: 10800.0
_____________________________
_____________________________Employee Salary Sheet: 0003
Employee Name: Hasini
Employee Position: AQA
Total Hours Worked: 150
Base Salary: 5000.0
Addition: 0.0
_____________________________
Total Salary: 5000.0
_____________________________
_____________________________Employee Salary Sheet: 0004
Employee Name: Hasini
Employee Position: QA
Total Hours Worked: 210
Base Salary: 6000.0
Addition: 2800.0
_____________________________
Total Salary: 8800.0
_____________________________
_____________________________
Again I have implemented the same scenario with JavaScript. First, I have created a class called EmployeeSalary by extending the Employee class and continuing the rest as previously. Refer to the following code.
EmployeeSalary {
id: '0001',
name: 'Hasini',
position: 'ASE',
hours: 200,
baseSalary: 5000,
addition: 0,
salary: 0
}
Here also I have used the same logic to implement this pattern again. Refer to the following code.
EmployeeSalary {
id: '0001',
name: 'Hasini',
position: 'ASE',
hours: 200,
baseSalary: 5000,
addition: 1500,
salary: 6500
}
EmployeeSalary {
id: '0002',
name: 'Hasini',
position: 'SE',
hours: 250,
baseSalary: 6000,
addition: 4800,
salary: 10800
}
EmployeeSalary {
id: '0003',
name: 'Hasini',
position: 'AQA',
hours: 150,
baseSalary: 5000,
addition: 0,
salary: 5000
}
EmployeeSalary {
id: '0004',
name: 'Hasini',
position: 'QA',
hours: 210,
baseSalary: 6000,
addition: 2800,
salary: 8800
}
This is the end of the article. I think you have got some understanding of the basics of the chain of responsibility design pattern. I hope to discuss another design pattern in my next article.
References