模拟学生请假流程,用以说明责任链模式。
请假天数 < 10,老师审批;
10 <= 请假天数 < 20,主任审批;
20 <= 请假天数 < 30,校长审批;
请假天数 >= 30,超出学校允许请假范围。
java工程包结构截图:
package test.designPattern.chainOfResponsibility;
/**
* 请假请求
* @author wangjiao01
*
*/
public class LeaveRequest {
private String name;
private String reason;
private Integer days;
public LeaveRequest(String name, String reason, Integer days){
super();
this.name = name;
this.reason = reason;
this.days = days;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public Integer getDays() {
return days;
}
public void setDays(Integer days) {
this.days = days;
}
}
package test.designPattern.chainOfResponsibility;
/**
* 领导角色
* @author wangjiao01
*
*/
public abstract class Leader {
protected String name;
/**
* 建立责任链关系的关键属性:下一个审批领导
*/
protected Leader nextLeader;
public Leader(String name){
super();
this.name = name;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Leader getNextLeader() {
return nextLeader;
}
/**
* 设置下一个审批领导
* @param nextLeader
*/
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
/**
* 处理请假
* @param leaveRequest
*/
public abstract void handleLeaveRequest(LeaveRequest leaveRequest);
}
package test.designPattern.chainOfResponsibility;
/**
* 老师角色
* @author wangjiao01
*
*/
public class Teacher extends Leader {
public Teacher(String name){
super(name);
}
@Override
public void handleLeaveRequest(LeaveRequest leaveRequest) {
Integer days = leaveRequest.getDays();
if(days < 10){
System.out.println(this.getName() + "处理了请假。");
}else{
this.nextLeader.handleLeaveRequest(leaveRequest);
}
}
}
package test.designPattern.chainOfResponsibility;
/**
* 主任角色
* @author wangjiao01
*
*/
public class Director extends Leader {
public Director(String name){
super(name);
}
@Override
public void handleLeaveRequest(LeaveRequest leaveRequest) {
Integer days = leaveRequest.getDays();
if(days < 20){
System.out.println(this.getName() + "处理了请假。");
}else{
this.nextLeader.handleLeaveRequest(leaveRequest);
}
}
}
package test.designPattern.chainOfResponsibility;
/**
* 校长角色
* @author wangjiao01
*
*/
public class HeadMaster extends Leader {
public HeadMaster(String name) {
super(name);
}
@Override
public void handleLeaveRequest(LeaveRequest leaveRequest) {
Integer days = leaveRequest.getDays();
if(days < 30){
System.out.println(this.getName() + "处理了请假。");
}else{
System.out.println("请假天数超出学校允许范围。");
}
}
}
package test.designPattern.chainOfResponsibility;
/**
* 测试方法
* @author wangjiao01
*
*/
public class TestMethod {
public static void main(String[] args) {
// 请假请求对象
LeaveRequest leaveRequest = new LeaveRequest("小明", "生病", 5);
// 创建责任链环节
Teacher teacher = new Teacher("老师");
Director director = new Director("主任");
HeadMaster headMaster = new HeadMaster("校长");
// 建立责任链关系
teacher.setNextLeader(director);
director.setNextLeader(headMaster);
// 处理请假请求
teacher.handleLeaveRequest(leaveRequest);
}
}
测试结果: