软件设计 石家庄铁道大学信息学院
实验15:职责链模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 提交类图;
2. 提交源代码;
// 采购单类
class PurchaseRequest {
private double amount;
private String purpose;
public PurchaseRequest(double amount, String purpose) {
this.amount = amount;
this.purpose = purpose;
}
public double getAmount() {
return amount;
}
public String getPurpose() {
return purpose;
}
}
// 审批者抽象类
abstract class Approver {
protected Approver successor;
public void setSuccessor(Approver successor) {
this.successor = successor;
}
public abstract void processRequest(PurchaseRequest request);
}
// 主任审批者类
class Director extends Approver {
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() <= 10000) {
System.out.println("主任审批通过,采购单金额:" + request.getAmount() + ",用途:" + request.getPurpose());
} else if (successor!= null) {
successor.processRequest(request);
}
}
}
// 部门经理审批者类
class DepartmentManager extends Approver {
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() <= 50000) {
System.out.println("部门经理审批通过,采购单金额:" + request.getAmount() + ",用途:" + request.getPurpose());
} else if (successor!= null) {
successor.processRequest(request);
}
}
}
// 副总经理审批者类
class ViceGeneralManager extends Approver {
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() <= 100000) {
System.out.println("副总经理审批通过,采购单金额:" + request.getAmount() + ",用途:" + request.getPurpose());
} else if (successor!= null) {
successor.processRequest(request);
}
}
}
// 总经理审批者类
class GeneralManager extends Approver {
@Override
public void processRequest(PurchaseRequest request) {
if (request.getAmount() <= 200000) {
System.out.println("总经理审批通过,采购单金额:" + request.getAmount() + ",用途:" + request.getPurpose());
} else {
System.out.println("采购单金额超过20万元,需要开职工大会确定,采购单金额:" + request.getAmount() + ",用途:" + request.getPurpose());
}
}
}
public class Main {
public static void main(String[] args) {
// 创建各级审批者对象
Approver director = new Director();
Approver departmentManager = new DepartmentManager();
Approver viceGeneralManager = new ViceGeneralManager();
Approver generalManager = new GeneralManager();
// 设置审批链顺序
director.setSuccessor(departmentManager);
departmentManager.setSuccessor(viceGeneralManager);
viceGeneralManager.setSuccessor(generalManager);
// 创建不同金额的采购单并进行审批
PurchaseRequest request1 = new PurchaseRequest(8000, "办公用品采购");
director.processRequest(request1);
PurchaseRequest request2 = new PurchaseRequest(30000, "设备采购");
director.processRequest(request2);
PurchaseRequest request3 = new PurchaseRequest(80000, "项目材料采购");
director.processRequest(request3);
PurchaseRequest request4 = new PurchaseRequest(150000, "大型设备采购");
director.processRequest(request4);
PurchaseRequest request5 = new PurchaseRequest(250000, "新业务拓展采购");
director.processRequest(request5);
}
}
3. 注意编程规范。