Sunny 软件公司的 OA 系统需要提供一个假条审批模块:如果员工请假天数小于 3 天,主任可以审批该请假条;如果员工请假天数大于或等于 3 天,小于 10 天,经理可以审批;如果员工请假天数大于或等于 10 天,小于 30 天,总经理可以审批;如果超过 30 天,总经理也不能审批,提示相应的拒绝信息。试用职责链模式设计该假条审批模块。
一、类结构图
抽象处理者类:设计为抽象类 Approver,持有一个 Approver 类型的成员 successor 的引用,包含设置后续者具体方法 setSuccessor()、处理审批逻辑的抽象方法 processApproval();
具体处理者类:设计为抽象处理者类的子类,具体实现处理审批逻辑的抽象方法 processApproval();
假条审批请求类:名称为 LeaveApproval,包含请假条的基本信息。
二、典型实现代码
抽象假条审批者类:抽象处理者类
// 抽象假条审批者类:抽象处理者类
public abstract class Approvor {
protected Approvor successor;
protected String name;
public Approvor(String name) {
this.name = name;
}
public void setSuccessor(Approvor successor) {
this.successor = successor;
}
public abstract void processApproval(LeaveApproval leaveApproval);
}
复制代码
主任审批者类:具体处理者类
// 主任审批者类:具体处理者类
public class Director extends Approvor {
public Director(String name) {
super(name);
}
@Override
public void processApproval(LeaveApproval leaveApproval) {
if (leaveApproval.getLeaveDays() < 3) {
System.out.println(name + "审批假条:通过");
} else {
successor.processApproval(leaveApproval);
}
}
}
复制代码
经理审批者类:具体处理者类
// 经理审批者类:具体处理者类
public class Manager extends Approvor {
public Manager(String name) {
super(name);
}
@Override
public void processApproval(LeaveApproval leaveApproval) {
if (leaveApproval.getLeaveDays() >= 3 && leaveApproval.getLeaveDays() < 10) {
System.out.println(name + "审批假条:通过");
} else {
successor.processApproval(leaveApproval);
}
}
}
复制代码
总经理审批者类:具体处理者类
// 总经理审批者类:具体处理者类
public class President extends Approvor {
public President(String name) {
super(name);
}
@Override
public void processApproval(LeaveApproval leaveApproval) {
if (leaveApproval.getLeaveDays() >= 10 && leaveApproval.getLeaveDays() < 30) {
System.out.println(name + "审批假条:通过");
} else {
System.out.println("不允许此类假条的审批,已被拒绝");
}
}
}
复制代码
假条审批请求类:
// 假条审批请求类
public class LeaveApproval {
private String name; // 请假姓名
private Date startDate; // 请假日期
private Integer leaveDays; // 请假天数
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Integer getLeaveDays() {
return leaveDays;
}
public void setLeaveDays(Integer leaveDays) {
this.leaveDays = leaveDays;
}
}
复制代码
客户端代码:
public class Client {
public static void main(String[] args) {
Approvor director, manager, president;
director = new Director("主任");
manager = new Manager("经理");
president = new President("总经理");
// 创建责任链
director.setSuccessor(manager);
manager.setSuccessor(president);
// 创建请假审批请求
LeaveApproval leaveApproval;
leaveApproval = new LeaveApproval();
leaveApproval.setName("张无忌");
leaveApproval.setStartDate(new Date());
leaveApproval.setLeaveDays(1);
director.processApproval(leaveApproval);
leaveApproval.setLeaveDays(5);
director.processApproval(leaveApproval);
leaveApproval.setLeaveDays(10);
director.processApproval(leaveApproval);
leaveApproval.setLeaveDays(30);
director.processApproval(leaveApproval);
}
}
复制代码
编译并运行程序,输出如下结果:
主任审批假条:通过
经理审批假条:通过
总经理审批假条:通过
不允许此类假条的审批,已被拒绝
复制代码
评论