实验15:职责链模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解职责链模式的动机,掌握该模式的结构;
2、能够利用职责链模式解决实际问题。
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:
1. 提交类图;
2. 提交源代码;
3. 注意编程规范。
1.
2.// 采购请求类
class PurchaseRequest {
private double amount;
private String reason;
public PurchaseRequest(double amount, String reason) {
this.amount = amount;
this.reason = reason;
}
public double getAmount() {
return amount;
}
public String getReason() {
return reason;
}
}
// 审批者抽象类
abstract class Approver {
protected Approver successor;
public Approver(Approver successor) {
this.successor = successor;
}
public void setSuccessor(Approver successor) {
this.successor = successor;
}
public abstract void requestApproval(PurchaseRequest request);
}
// 主任类
class Director extends Approver {
public Director(Approver successor) {
super(successor);
}
@Override
public void requestApproval(PurchaseRequest request) {
if (request.getAmount() <= 10000) {
System.out.println("Director approves the request: " + request.getReason());
} else if (successor != null) {
successor.requestApproval(request);
}
}
}
// 部门经理类
class DepartmentManager extends Approver {
public DepartmentManager(Approver successor) {
super(successor);
}
@Override
public void requestApproval(PurchaseRequest request) {
if (request.getAmount() > 10000 && request.getAmount() <= 50000) {
System.out.println("Department Manager approves the request: " + request.getReason());
} else if (successor != null) {
successor.requestApproval(request);
}
}
}
// 副总经理类
class DeputyGeneralManager extends Approver {
public DeputyGeneralManager(Approver successor) {
super(successor);
}
@Override
public void requestApproval(PurchaseRequest request) {
if (request.getAmount() > 50000 && request.getAmount() <= 100000) {
System.out.println("Deputy General Manager approves the request: " + request.getReason());
} else if (successor != null) {
successor.requestApproval(request);
}
}
}
// 总经理类
class GeneralManager extends Approver {
public GeneralManager(Approver successor) {
super(successor);
}
@Override
public void requestApproval(PurchaseRequest request) {
if (request.getAmount() > 100000 && request.getAmount() <= 200000) {
System.out.println("General Manager approves the request: " + request.getReason());
} else if (successor != null) {
successor.requestApproval(request);
}
}
}
// 职工大会类
class StaffMeeting extends Approver {
public StaffMeeting() {
super(null);
}
@Override
public void requestApproval(PurchaseRequest request) {
if (request.getAmount() > 200000) {
System.out.println("Staff Meeting approves the request: " + request.getReason());
} else {
System.out.println("No approval needed for this request.");
}
}
}
// 客户端测试类
public class ApprovalSystem {
public static void main(String[] args) {
Approver staffMeeting = new StaffMeeting();
Approver generalManager = new GeneralManager(staffMeeting);
Approver deputyGeneralManager = new DeputyGeneralManager(generalManager);
Approver departmentManager = new DepartmentManager(deputyGeneralManager);
Approver director = new Director(departmentManager);
PurchaseRequest request1 = new PurchaseRequest(5000, "Laptop for Marketing Team");
PurchaseRequest request2 = new PurchaseRequest(15000, "Office renovation");
PurchaseRequest request3 = new PurchaseRequest(80000, "New software license");
PurchaseRequest request4 = new PurchaseRequest(250000, "Expansion project");
director.requestApproval(request1);
director.requestApproval(request2);
director.requestApproval(request3);
director.requestApproval(request4);
}
}