OA系统采购审批需求
学校OA系统的采购审批项目: 需求是
- 采购员采购教学器材
- 如果金额 小于等于5000,由教学主任审批
- 如果金额 小于等于10000,由院长审批
- 如果金额 小于等于30000, 由副校长审批
- 如果金额 超过30000以上,有校长审批
责任链模式基本介绍:
基本介绍:
- 职责链模式(Chain of Responsibility Pattern), 又叫责任链模式,为请求创建了一个接受者对象的链。这种模式对请求的发送者和接收者进行解耦。
- 责任链模式通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依次类推
3)这种类型的设计模式属于行为型模式。
public abstract class Approver {
//下一个处理者
Approver approver;
String name;
public Approver(String name){
this.name = name;
}
// 下一个处理者
public void setApprover(Approver approver){
this.approver = approver;
}
//处理审批请求的方法,得到一个请求,处理是子类完成,因此该方法做成抽象
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
public class SchoolMasterApprover extends Approver{
public SchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() >= 30000) {
System.out.println("请求编号 id = " + purchaseRequest.getId() + " 被" + this.name + " 处理");
} else{
approver.processRequest(purchaseRequest);
}
}
}
public class ViceSchoolMasterApprover extends Approver{
public ViceSchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() >= 10000 && purchaseRequest.getPrice() < 30000) {
System.out.println("请求编号 id = " + purchaseRequest.getId() + " 被" + this.name + " 处理");
} else{
approver.processRequest(purchaseRequest);
}
}
}
public class CollegeApprover extends Approver{
public CollegeApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() >= 5000 && purchaseRequest.getPrice() < 10000) {
System.out.println("请求编号 id = " + purchaseRequest.getId() + " 被" + this.name + " 处理");
} else{
approver.processRequest(purchaseRequest);
}
}
}
public class DepartmentApprover extends Approver{
public DepartmentApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() <= 5000){
System.out.println("请求编号 id = " + purchaseRequest.getId() + " 被" + this.name + " 处理");
} else{
approver.processRequest(purchaseRequest);
}
}
}
public class PurchaseRequest {
private int type; // 请求类型
private float price = 0.0f;
private int id = 0;
public PurchaseRequest(int type,float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public float getPrice() {
return price;
}
public int getType() {
return type;
}
public int getId() {
return id;
}
}
public class Client {
public static void main(String[] args) {
//创建一个请求
PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);
//创建相关的审批人
DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
CollegeApprover collegeApprover = new CollegeApprover("张院长");
ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("张副校长");
SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("张校长");
departmentApprover.setApprover(collegeApprover);
collegeApprover.setApprover(viceSchoolMasterApprover);
viceSchoolMasterApprover.setApprover(schoolMasterApprover);
schoolMasterApprover.setApprover(departmentApprover);
departmentApprover.processRequest(purchaseRequest);
viceSchoolMasterApprover.processRequest(purchaseRequest);
}
}
测试: