实验15:职责链模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 提交类图;
2. 提交源代码;
public class Client {
public static void main(String[] args) {
PurchaseRequest purchaseRequest1 = new PurchaseRequest(1, 5600, 1);
PurchaseRequest purchaseRequest2 = new PurchaseRequest(1, 25620, 2);
PurchaseRequest purchaseRequest3 = new PurchaseRequest(1, 96541, 3);
PurchaseRequest purchaseRequest4 = new PurchaseRequest(1, 152345, 4);
PurchaseRequest purchaseRequest5 = new PurchaseRequest(1, 235685, 5);
Director director = new Director("主任");
DepartmentManager departmentManager = new DepartmentManager("部门经理");
ViceManager viceManager = new ViceManager("副总经理");
GeneralManager generalManager = new GeneralManager("总经理");
director.setApprover(departmentManager);
departmentManager.setApprover(viceManager);
viceManager.setApprover(generalManager);
System.out.println(purchaseRequest1.toString());
director.processRequest(purchaseRequest1);
System.out.println("-------------------------------");
System.out.println(purchaseRequest2.toString());
director.processRequest(purchaseRequest2);
System.out.println("-------------------------------");
System.out.println(purchaseRequest3.toString());
director.processRequest(purchaseRequest3);
System.out.println("-------------------------------");
System.out.println(purchaseRequest4.toString());
director.processRequest(purchaseRequest4);
System.out.println("-------------------------------");
System.out.println(purchaseRequest5.toString());
director.processRequest(purchaseRequest5);
System.out.println("-------------------------------");
}
}
public class PurchaseRequest {
private int type;
private float price;
private int id;
public PurchaseRequest(int type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public int getType() {
return type;
}
@Override
public String toString() {
return this.getId() + "号物资" + this.getPrice() + "元";
}
public float getPrice() {
return price;
}
public int getId() {
return id;
}
}
public class ViceManager extends Approver {
public ViceManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 50000 && purchaseRequest.getPrice() <= 100000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
public class GeneralManager extends Approver {
public GeneralManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 100000 && purchaseRequest.getPrice() <= 200000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
System.out.println("需要开员工大会确定");
}
}
}
public class Director extends Approver {
public Director(String name) {
super(name);
}
@Override
public void setApprover(Approver approver) {
super.setApprover(approver);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() <= 10000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
public class DepartmentManager extends Approver {
public DepartmentManager(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 50000) {
System.out.println("请求编号id => " + purchaseRequest.getId() + "被" + this.position + "处理");
} else {
approver.processRequest(purchaseRequest);
}
}
}
public abstract class Approver {
// 下一个后继者
Approver approver;
// 职位
String position;
public Approver(String position) {
this.position = position;
}
// 下一个处理者
public void setApprover(Approver approver) {
this.approver = approver;
}
// 处理审批请求的方法
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
3. 注意编程规范。