Chain of Responsibility Design Pattern in Java
Posted By : Akash Goyal | 16-May-2018
Chain of responsibility design is utilized to accomplish loose coupling in programming outline where a demand from the customer is passed to a chain of responsibility to process them. At that point the protest in the chain will choose themselves will's identity preparing the demand and whether the demand is required to be sent to the following article in the chain or not.
Chain of Responsibility Pattern Example in JDK
We should see the case of a chain of duty design in JDK and after that, we will continue to actualize a genuine case of this example. We realize that we can have various catch block in a try-catch block code. Here each catch square is somewhat of a processor to process that specific exception.
So when any special case happens in the attempt hinder, its send to the main catch piece to process. On the off chance that the catch piece can't process it, it advances the demand to next question in chain i.e next catch square. On the off chance that even the last catch piece can't process it, the special case is tossed outside of the affix to the calling program.
Chain of Responsibility Design Pattern Example
1>One of the great examples of Chain of responsibility design is ATM machine. The client enters the sum to be apportioned and the machine administers sum regarding characterized cash bills, for example, 50$, 20$, 10$ and so on.
2>Example of Chain of Responsibility pattern is left Policy Approve. Employee wants to apply to leave then according to criteria leave approve by successor.please see below implementation.
package designpattern.chainresponsibility;
public abstract class Employee1 {
protected Employee1 nextsupervisor;
public void setNextSupervisor( Employee1 supervisor )
{
this.nextsupervisor = supervisor;
}
public abstract void applyLeave( String employeefullName, int numberofLeaveDays );
}
package designpattern.chainresponsibility;
public class TeamLeader extends Employee
{
// TeamLeader can only approve up to 10 days of leave
private int MAX_LEAVES_CAN_APPROVE = 10;
public void applyLeave( String employeeName, int numberofDaysLeave )
{
// check if TeamLeader can process this request
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
// if TeamLeader can't process the LeaveRequest then pass on to the supervisor(ProjectLeader)
// so that he can process
else
{
nextsupervisor.applyLeave(employeeName, numberofDaysLeave);
}
}
//Approve Leave implementation
private void ApproveLeave( String employeeName, int numberofDaysLeave )
{
System.out.println("TeamLeader approved " + numberofDaysLeave + " days " + "Leave for the employee : "
+ employeeName);
}
}
package designpattern.chainresponsibility;
public class ProjectLeader extends Employee
{
// ProjectLeader can only approve up to Below days of leave
private int MAX_LEAVES_CAN_APPROVE = 20;
public void applyLeave( String employeeName, int numberofDaysLeave )
{
// check if ProjectLeader leave request criteria
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
// if ProjectLeader can't process the LeaveRequest then pass on to the supervisor(HR)
// so that he can process
else
{
nextsupervisor.applyLeave(employeeName, numberofDaysLeave);
}
}
//if eligible leave request criteria
private void ApproveLeave( String employeeName, int numberofDaysLeave )
{
System.out.println("ProjectLeader approved [" + numberofDaysLeave + " ]days " + "Leave for the employee : ["
+ employeeName+"]");
}
}
package designpattern.chainresponsibility;
public class HR extends Employee{
// HR can only approve up to 30 days of leave
private int MAX_LEAVES_CAN_APPROVE = 30;
public void applyLeave( String employeeName, int numberofDaysLeave )
{
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
else
{
System.out.println("Leave application suspended, Please contact HR because of leave criteria");
}
}
private void ApproveLeave( String employeeName, int numberofDaysLeave )
{
System.out.println("HR approved [" + numberofDaysLeave + "] days " + "Leave for the employee : ["
+ employeeName+"]");
}}
package designpattern.chainresponsibility;
public class ChainPatternDemo
{
public static void main( String[] args )
{
TeamLeader teamLeader= new TeamLeader();
ProjectLeader projectLeader= new ProjectLeader();
HR hr=new HR();
teamLeader.setNextSupervisor(projectLeader);
projectLeader.setNextSupervisor(hr);
teamLeader.applyLeave("Piyush", 9);
System.out.println();
teamLeader.applyLeave("Arun", 18);
System.out.println();
teamLeader.applyLeave("Amit", 30);
System.out.println();
teamLeader.applyLeave("Akash", 50);
}
}
output
TeamLeader approved 9 days Leave for the employee : Piyush
ProjectLeader approved [18 ]days Leave for the employee : [Arun]
HR approved [30] days Leave for the employee : [Amit]
Leave application suspended, Please contact HR because of leave criteria
Chain of Responsibility Design Pattern Important Points
• A customer doesn't know which part of the chain will process the demand and it will send the demand to the principal question in the chain. For instance, in our program LeavePolicy is ignorant of who is handling the demand to support the leave ask for as indicated by leave apply.
• Each question in the chain will have its own particular usage to process the demand, either full or halfway or to send it to the following article in the chain.
• Each protest in the fasten ought to have reference to the following item in an anchor to forward the demand to, its accomplished by java organization.
• Making the chain precisely is essential generally there may be a case that the demand will never be sent to a specific processor or there are no items in the anchor who can deal with the demand. In my usage, I have included the check for the client entered sum to ensure it gets handled completely by every one of the processors however we won't check it and toss special case if the demand achieves the last question and there are no further protests in the tie to forward the demand to. This is a plan choice.
• Chain of Duty configuration design regards accomplishes lose coupling yet it accompanies the exchange off of having a considerable measure of usage classes and support issues if a large portion of the code is regular in every one of the executions.
Chain of Responsibility Pattern Examples in JDK
-
Chain of responsibility design is utilized to accomplish loose coupling in programming outline where a demand from the customer is passed to a chain of responsibility to process them. At that point the protest in the chain will choose themselves will's identity preparing the demand and whether the demand is required to be sent to the following article in the chain or not.
Chain of Responsibility Pattern Example in JDK
We should see the case of a chain of duty design in JDK and after that, we will continue to actualize a genuine case of this example. We realize that we can have various catch block in a try-catch block code. Here each catch square is somewhat of a processor to process that specific exception.
So when any special case happens in the attempt hinder, its send to the main catch piece to process. On the off chance that the catch piece can't process it, it advances the demand to next question in chain i.e next catch square. On the off chance that even the last catch piece can't process it, the special case is tossed outside of the affix to the calling program.
Chain of Responsibility Design Pattern Example
1>One of the great examples of Chain of responsibility design is ATM machine. The client enters the sum to be apportioned and the machine administers sum regarding characterized cash bills, for example, 50$, 20$, 10$ and so on.
2>Example of Chain of Responsibility pattern is left Policy Approve. Employee wants to apply to leave then according to criteria leave approve by successor.please see below implementation.
package designpattern.chainresponsibility;
public abstract class Employee1 {
protected Employee1 next supervisor;
public void setNextSupervisor( Employee1 supervisor )
{
this.nextsupervisor = supervisor;
}
public abstract void applyLeave( String employeefullName, int numberofLeaveDays );}
package designpattern.chainresponsibility;public class TeamLeader extends Employee
{
// TeamLeader can only approve up to 10 days of leave
private int MAX_LEAVES_CAN_APPROVE = 10;
public void applyLeave( String employeeName, int numberofDaysLeave ){
// check if TeamLeader can process this request
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
// if TeamLeader can't process the LeaveRequest then pass on to the supervisor(ProjectLeader)
// so that he can process
else
{
nextsupervisor.applyLeave(employeeName, numberofDaysLeave);
}
}
//Approve Leave implementationprivate void ApproveLeave( String employeeName, int numberofDaysLeave )
{
System.out.println("TeamLeader approved " + numberofDaysLeave + " days " + "Leave for the employee : "
+ employeeName);
}}
package designpattern.chainresponsibility;
public class ProjectLeader extends Employee
{
// ProjectLeader can only approve up to Below days of leave
private int MAX_LEAVES_CAN_APPROVE = 20;
public void applyLeave( String employeeName, int numberofDaysLeave ){
// check if ProjectLeader leave request criteria
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
// if ProjectLeader can't process the LeaveRequest then pass on to the supervisor(HR)
// so that he can process
else
{
nextsupervisor.applyLeave(employeeName, numberofDaysLeave);
}
}
//if eligible leave request criteriaprivate void ApproveLeave( String employeeName, int numberofDaysLeave )
{
System.out.println("ProjectLeader approved [" + numberofDaysLeave + " ]days " + "Leave for the employee : ["
+ employeeName+"]");
}}
package designpattern.chainresponsibility;public class HR extends Employee{
// HR can only approve up to 30 days of leave
private int MAX_LEAVES_CAN_APPROVE = 30;
public void applyLeave( String employeeName, int numberofDaysLeave )
{
if( numberofDaysLeave <= MAX_LEAVES_CAN_APPROVE )
{
ApproveLeave(employeeName, numberofDaysLeave);
}
else
{
System.out.println("Leave application suspended, Please contact HR because of leave criteria");
}
}
private void ApproveLeave( String employeeName, int numberofDaysLeave ){
System.out.println("HR approved [" + numberofDaysLeave + "] days " + "Leave for the employee : ["
+ employeeName+"]");
}}
package designpattern.chainresponsibility;
public class ChainPatternDemo
{
public static void main( String[] args ){
TeamLeader teamLeader= new TeamLeader();
ProjectLeader projectLeader= new ProjectLeader();
HR hr=new HR();
teamLeader.setNextSupervisor(projectLeader);
projectLeader.setNextSupervisor(hr);
teamLeader.applyLeave("Piyush", 9);System.out.println();
teamLeader.applyLeave("Arun", 18);
System.out.println();
teamLeader.applyLeave("Amit", 30);
System.out.println();
teamLeader.applyLeave("Akash", 50);
}
}output
TeamLeader approved 9 days Leave for the employee: Piyush
ProjectLeader approved [18 ]days Leave for the employee : [Arun]
HR approved [30] days Leave for the employee : [Amit]
Leave application suspended, Please contact HR because of leave criteria
Chain of Responsibility Design Pattern Important Points
• A customer doesn't know which part of the chain will process the demand and it will send the demand to the principal question in the chain. For instance, in our program LeavePolicy is ignorant of who is handling the demand to support the leave ask for as indicated by leave apply.• Each question in the chain will have its own particular usage to process the demand, either full or halfway or to send it to the following article in the chain.
• Each protest in the fasten ought to have reference to the following item in an anchor to forward the demand to, its accomplished by java organization.
• Making the chain precisely is essential generally there may be a case that the demand will never be sent to a specific processor or there are no items in the anchor who can deal with the demand. In my usage, I have included the check for the client entered sum to ensure it gets handled completely by every one of the processors however we won't check it and toss special case if the demand achieves the last question and there are no further protests in the tie to forward the demand to. This is a plan choice.
logger
-
doFileter
That's all about the Chain of Responsibility design pattern, I hope you doubt clear about a chain of responsibility design pattern.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Goyal
Akash is a software engineer and good working experience in Java, rest and soap API, SQL, data structure and algorithm and learns new technologies. His hobbies are solving a puzzle and playing chess etc.