Answered step by step
Verified Expert Solution
Link Copied!

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 setNextApprover(LeaveApprover nextApprover);
void approveLeave(LeaveRequest leaveRequest);
}
Classes:
package ChainOfResponsibiltyDemo;
// LeaveRequest class
class LeaveRequest {
private int days;
public LeaveRequest(int days){
this.days = days;
}
public int getDays(){
return days;
}
}
package ChainOfResponsibiltyDemo;
class TeamLead implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=5){
System.out.println("Leave approved by Team Lead");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
package ChainOfResponsibiltyDemo;
class DepartmentManager implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=10){
System.out.println("Leave approved by Department Manager");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
package ChainOfResponsibiltyDemo;
class HRManager implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=15){
System.out.println("Leave approved by HR Manager");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
Driver
package ChainOfResponsibiltyDemo;
public class CoRDriver {
public static void main(String[] args){
LeaveApprover teamLead = new TeamLead();
LeaveApprover departmentManager = new DepartmentManager();
LeaveApprover hrManager = new HRManager();
// Set up the chain of responsibility
teamLead.setNextApprover(departmentManager);
departmentManager.setNextApprover(hrManager);
// Create a leave request - test with 4,14 and 20 to see the results of the approval process
LeaveRequest leaveRequest = new LeaveRequest(8);
// Send the leave request for approval
teamLead.approveLeave(leaveRequest);
}
}
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 5 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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

Students also viewed these Databases questions