员工请假一天需要组长审批请假三天需要经理审批,请假七天需要老板审批,无权审批时转交给上级领导,用java设计模式责任链模式编写,给出完整代码
时间: 2024-03-01 13:53:00 浏览: 166
好的,以下是使用Java设计模式责任链模式实现员工请假审批的完整代码:
LeaveRequest.java:
```java
public class LeaveRequest {
private String name;
private int days;
public LeaveRequest(String name, int days) {
this.name = name;
this.days = days;
}
public String getName() {
return name;
}
public int getDays() {
return days;
}
}
```
LeaveRequestHandler.java:
```java
public abstract class LeaveRequestHandler {
protected LeaveRequestHandler successor;
public void setSuccessor(LeaveRequestHandler successor) {
this.successor = successor;
}
public abstract void handleRequest(LeaveRequest leaveRequest);
}
```
GroupLeaderHandler.java:
```java
public class GroupLeaderHandler extends LeaveRequestHandler {
@Override
public void handleRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getDays() <= 1) {
System.out.println("Group Leader approved the leave request for " + leaveRequest.getDays() + " day(s).");
} else {
if (successor != null) {
successor.handleRequest(leaveRequest);
}
}
}
}
```
ManagerHandler.java:
```java
public class ManagerHandler extends LeaveRequestHandler {
@Override
public void handleRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getDays() > 1 && leaveRequest.getDays() <= 3) {
System.out.println("Manager approved the leave request for " + leaveRequest.getDays() + " day(s).");
} else {
if (successor != null) {
successor.handleRequest(leaveRequest);
}
}
}
}
```
BossHandler.java:
```java
public class BossHandler extends LeaveRequestHandler {
@Override
public void handleRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getDays() > 3 && leaveRequest.getDays() <= 7) {
System.out.println("Boss approved the leave request for " + leaveRequest.getDays() + " day(s).");
} else {
System.out.println("Sorry, your leave request is rejected.");
}
}
}
```
LeaveRequestTest.java:
```java
public class LeaveRequestTest {
public static void main(String[] args) {
LeaveRequest leaveRequest = new LeaveRequest("Tom", 2);
GroupLeaderHandler groupLeaderHandler = new GroupLeaderHandler();
ManagerHandler managerHandler = new ManagerHandler();
BossHandler bossHandler = new BossHandler();
groupLeaderHandler.setSuccessor(managerHandler);
managerHandler.setSuccessor(bossHandler);
groupLeaderHandler.handleRequest(leaveRequest);
}
}
```
这个例子中,Tom申请了两天的请假,程序会先将请求交给组长处理,由于请假时间超过了一天,组长就会将请求转交给经理处理,经理再将请求转交给老板处理,最终老板审批通过了Tom的请假请求。如果Tom申请了超过7天的请假,无论是哪个处理者,都会将请求拒绝。如果Tom申请了1天以内的请假,只有组长会处理请求,其他处理者不会处理。如果Tom申请了3天以内的请假,组长和经理会处理请求,老板不会处理。
阅读全文