请求类
public class ApproverRequest {private int type;//请求批准的类型private float price;//请求的金额private int id;//请求的编号
}
审批人抽象类
public abstract class ApproverPerson {protected ApproverPerson next;protected String name;//审批过程public abstract void processRequest(ApproverRequest request);
}
教导主任类
public class DepartmentApprover extends ApproverPerson {@Overridepublic void processRequest(ApproverRequest request) {System.out.println("教导主任正在处理批准...");if (request.getPrice() <= 5000) {System.out.println("请求编号 id= " + request.getId() + " 被教导主任 " + this.name + " 处理");} else {System.out.println("金额过大,教导主任处理不了,交给院长处理...");next.processRequest(request);}}
}
院长类
public class CollegeApprover extends ApproverPerson {@Overridepublic void processRequest(ApproverRequest request) {System.out.println("院长正在处理批准...");if (request.getPrice() > 5000 && request.getPrice() <= 10000) {System.out.println("请求编号 id= " + request.getId() + " 被院长 " + this.name + " 处理");} else {System.out.println("金额过大,院长处理不了,交给副校长处理...");next.processRequest(request);}}
}
副校长类
public class ViceSchoolMasterApprover extends ApproverPerson {@Overridepublic void processRequest(ApproverRequest request) {System.out.println("副校长正在处理批准...");if (request.getPrice() > 10000 && request.getPrice() <= 15000) {System.out.println("请求编号 id= " + request.getId() + " 被副校长 " + this.name + " 处理");} else {System.out.println("金额过大,副校长处理不了,交给校长处理...");next.processRequest(request);}}
}
校长类
public class SchoolMasterApprover extends ApproverPerson {@Overridepublic void processRequest(ApproverRequest request) {System.out.println("校长正在处理批准...");System.out.println("请求编号 id= " + request.getId() + " 被校长 " + this.name + " 处理");}
}
演示类
public class Demo {public static void main(String[] args) {ApproverRequest request = new ApproverRequest(1, 20000, 1001);ApproverPerson department = new DepartmentApprover("张主任");ApproverPerson college = new CollegeApprover("李院长");ApproverPerson viceSchoolMaster = new ViceSchoolMasterApprover("王副校长");ApproverPerson schoolMaster = new SchoolMasterApprover("佟校长");//设置当前审批人的指针,指向下一个审批人department.setNext(college);college.setNext(viceSchoolMaster);viceSchoolMaster.setNext(schoolMaster);department.processRequest(request);}
}
演示结果
为了使博客的代码更简洁,我把构造方法和set/get方法省略了