Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( java ) Interface: package ChainOfResponsibiltyDemo; interface LeaveApprover { void setNextApprover ( LeaveApprover nextApprover ) ; void approveLeave ( LeaveRequest leaveRequest ) ; } Classes:
java
Interface:
package ChainOfResponsibiltyDemo;
interface LeaveApprover
void setNextApproverLeaveApprover nextApprover;
void approveLeaveLeaveRequest leaveRequest;
Classes:
package ChainOfResponsibiltyDemo;
LeaveRequest class
class LeaveRequest
private int days;
public LeaveRequestint days
this.days days;
public int getDays
return days;
package ChainOfResponsibiltyDemo;
class TeamLead implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by Team Lead";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
package ChainOfResponsibiltyDemo;
class DepartmentManager implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by Department Manager";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
package ChainOfResponsibiltyDemo;
class HRManager implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by HR Manager";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
Driver
package ChainOfResponsibiltyDemo;
public class CoRDriver
public static void mainString args
LeaveApprover teamLead new TeamLead;
LeaveApprover departmentManager new DepartmentManager;
LeaveApprover hrManager new HRManager;
Set up the chain of responsibility
teamLead.setNextApproverdepartmentManager;
departmentManager.setNextApproverhrManager;
Create a leave request test with and to see the results of the approval process
LeaveRequest leaveRequest new LeaveRequest;
Send the leave request for approval
teamLead.approveLeaveleaveRequest;
In this example, each handler TeamLead DepartmentManager, and HRManager represents a level of authority in the leave approval process. Each handler implements the LeaveApprover interface and has a reference to the next approver in the chain.
When a leave request is submitted, it is passed to the first handler in the chain teamLead If the team lead has the authority to approve the leave in this case, for up to days the leave is approved. Otherwise, the request is passed to the next handler departmentManager The process continues until an approver can handle the request, or the request is rejected if it reaches the end of the chain.
By using the Chain of Responsibility pattern, the leave approval process is structured, and the responsibility for approving a leave request is delegated from one handler to another. Each handler has the flexibility to approve or reject the request based on its own criteria. This pattern promotes loose coupling between the sender of the request and its receivers, allowing for easy modification or extension of the approval process.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started