软件设计-Tutorial15
```mermaid classDiagram Approver <|-- Director Approver <|-- DepartmentManager Approver <|-- VicePresident Approver <|-- President Approver <|-- StaffMeeting Approver --> Approver : nextApprover class Approver { -Approver nextApprover -String name +setNextApprover(Approver) +approveRequest(PurchaseRequest) } class Director { +approveRequest(PurchaseRequest) } class DepartmentManager { +approveRequest(PurchaseRequest) } class VicePresident { +approveRequest(PurchaseRequest) } class President { +approveRequest(PurchaseRequest) } class StaffMeeting { +approveRequest(PurchaseRequest) } class PurchaseRequest { -double amount -String purpose +getAmount() +getPurpose() } Approver o-- PurchaseRequest : uses ```
// 抽象类:定义一个抽象的审批者 abstract class Approver { protected Approver nextApprover; // 下一个审批者 protected String name; // 审批者的名称 public Approver(String name) { this.name = name; } // 设置下一个审批者 public void setNextApprover(Approver nextApprover) { this.nextApprover = nextApprover; } // 抽象方法:处理采购请求 public abstract void approveRequest(PurchaseRequest request); } // 采购请求类 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; } } // 主任审批类 class Director extends Approver { public Director(String name) { super(name); } @Override public void approveRequest(PurchaseRequest request) { if (request.getAmount() <= 10000) { System.out.println("主任 " + name + " 批准了采购单:金额 " + request.getAmount() + " 元,用途:" + request.getPurpose()); } else if (nextApprover != null) { nextApprover.approveRequest(request); } } } // 部门经理审批类 class DepartmentManager extends Approver { public DepartmentManager(String name) { super(name); } @Override public void approveRequest(PurchaseRequest request) { if (request.getAmount() <= 50000) { System.out.println("部门经理 " + name + " 批准了采购单:金额 " + request.getAmount() + " 元,用途:" + request.getPurpose()); } else if (nextApprover != null) { nextApprover.approveRequest(request); } } } // 副总经理审批类 class VicePresident extends Approver { public VicePresident(String name) { super(name); } @Override public void approveRequest(PurchaseRequest request) { if (request.getAmount() <= 100000) { System.out.println("副总经理 " + name + " 批准了采购单:金额 " + request.getAmount() + " 元,用途:" + request.getPurpose()); } else if (nextApprover != null) { nextApprover.approveRequest(request); } } } // 总经理审批类 class President extends Approver { public President(String name) { super(name); } @Override public void approveRequest(PurchaseRequest request) { if (request.getAmount() <= 200000) { System.out.println("总经理 " + name + " 批准了采购单:金额 " + request.getAmount() + " 元,用途:" + request.getPurpose()); } else if (nextApprover != null) { nextApprover.approveRequest(request); } } } // 职工大会审批类 class StaffMeeting extends Approver { public StaffMeeting() { super("职工大会"); } @Override public void approveRequest(PurchaseRequest request) { System.out.println("采购单金额 " + request.getAmount() + " 元,需要召开职工大会进行审批。"); } } // 测试类 public class Main { public static void main(String[] args) { // 创建审批者对象 Approver director = new Director("张主任"); Approver departmentManager = new DepartmentManager("李经理"); Approver vicePresident = new VicePresident("王副总"); Approver president = new President("赵总"); Approver staffMeeting = new StaffMeeting(); // 构建职责链 director.setNextApprover(departmentManager); departmentManager.setNextApprover(vicePresident); vicePresident.setNextApprover(president); president.setNextApprover(staffMeeting); // 创建采购请求 PurchaseRequest request1 = new PurchaseRequest(8000, "办公用品采购"); PurchaseRequest request2 = new PurchaseRequest(30000, "项目设备采购"); PurchaseRequest request3 = new PurchaseRequest(70000, "大型设备采购"); PurchaseRequest request4 = new PurchaseRequest(150000, "建筑材料采购"); PurchaseRequest request5 = new PurchaseRequest(300000, "重大项目采购"); // 发起审批请求 director.approveRequest(request1); director.approveRequest(request2); director.approveRequest(request3); director.approveRequest(request4); director.approveRequest(request5); } }