本次责任链采用简单的请假申请来处理。
领导关系: 主任<副总监<总监
//一次请假请求类
public class LeaveRequest {
private String leaveName; //请假者的姓名
private int leaveDays; //请假的天数
private String reason; //请假的原因
public String getLeaveName() {
return leaveName;
}
public void setLeaveName(String leaveName) {
this.leaveName = leaveName;
}
public int getLeaveDays() {
return leaveDays;
}
public void setLeaveDays(int leaveDays) {
this.leaveDays = leaveDays;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
//领导的抽象类
public abstract class Leader {
//需要子类继承,所以使用protected
protected String leaderName; //领导的姓名
protected Leader nextLeader; //写一个领导对象
//通过构造方法注入领导的姓名或者也可以添加其他信息
public Leader(String leaderName){
this.leaderName = leaderName;
}
//采用set方法注入下一个领导
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
//处理请假请求业务的抽象方法
public abstract void headerRequest(LeaveRequest leaveRequest);
}
//主任
public class Director extends Leader{
public Director(String leaderName) {
super(leaderName);
}
@Override
public void headerRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getLeaveDays() < 10) { //主任只处理小于10天的请假请求
System.out.println("主任批");
System.out.println(leaveRequest.getLeaveName()+",请假"+leaveRequest.getLeaveDays()+"天,原因:"+leaveRequest.getReason());
} else if(this.nextLeader != null){ //大于10天的交给下一个领导处理,首先需要判断下一个领导是否存在
this.nextLeader.headerRequest(leaveRequest);
}
}
}
//副总监
public class DeputyManager extends Leader{
public DeputyManager(String leaderName) {
super(leaderName);
}
@Override
public void headerRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getLeaveDays() < 20) { //副总监只处理小于20天的请假请求
System.out.println("副总监批");
System.out.println(leaveRequest.getLeaveName()+",请假"+leaveRequest.getLeaveDays()+"天,原因:"+leaveRequest.getReason());
} else if(this.nextLeader != null){ //大于20天的交给下一个领导处理,首先需要判断下一个领导是否存在
this.nextLeader.headerRequest(leaveRequest);
}
}
}
//总监
public class Manager extends Leader{
public Manager(String leaderName) {
super(leaderName);
}
@Override
public void headerRequest(LeaveRequest leaveRequest) {
if (leaveRequest.getLeaveDays() < 30) { //总监只处理小于30天的请假请求
System.out.println("总监批");
System.out.println(leaveRequest.getLeaveName()+",请假"+leaveRequest.getLeaveDays()+"天,原因:"+leaveRequest.getReason());
} else{
System.out.println("总监未批");
System.out.println(leaveRequest.getLeaveName()+"难道想辞职?请"+leaveRequest.getLeaveDays()+"多天");
}
}
}
//流程处理类
public class Leave {
public static void main(String[] args) {
//设置一个请假请求
LeaveRequest employee01 = new LeaveRequest();
employee01.setLeaveName("员工张三");
employee01.setLeaveDays(4); //应交给 Director主任 处理
employee01.setReason("事假");
LeaveRequest employee02 = new LeaveRequest();
employee02.setLeaveName("员工李四");
employee02.setLeaveDays(15); //应交给 DeputyManager副总监 处理
employee02.setReason("事假");
LeaveRequest employee03 = new LeaveRequest();
employee03.setLeaveName("员工王五"); //应交给 Manager总监 处理
employee03.setLeaveDays(25);
employee03.setReason("事假");
LeaveRequest employee04 = new LeaveRequest();
employee04.setLeaveName("员工赵六");
employee04.setLeaveDays(40); //应交给Manager处理
employee04.setReason("事假");
//设置领导
Leader director = new Director("director"); //主任
Leader deputyManager = new DeputyManager("deputyManager"); //副总监
Leader manager = new Manager("manager"); //总监
//设置各个领导的上下级关系
director.setNextLeader(deputyManager);
deputyManager.setNextLeader(manager);
//从主任开始处理
director.headerRequest(employee01);
director.headerRequest(employee02);
director.headerRequest(employee03);
director.headerRequest(employee04);
}
}
结果:
类图:
温馨注意:
1、流程处理类中的关于领导的设置以及领导的上下级关系可以提取出来放到配置文件中配置,通过反射处理;
2、责任链模式的好处是避免了纯 if else语句的臃肿,而且可以在随意增加链的处理环节,而不需要改变原代码,做到了解耦;
3、其实还可以将本次测试中的总监副总监主任等的具体处理者放置在链表Linked的一个个对象中,也可以放置在数组的一个个对象中。