职责链模式Java代码编写:
整体布局如下:
(1)Approver.java:
package shiyan15; public abstract class Approver { //抽象处理者 protected Approver successor;// 后继对象 protected String name; public Approver(String name) { this.name = name; } // 设置后继者 public void setSuccessor(Approver successor) { this.successor = successor; } // 抽象请求处理方法 public abstract void processRequest(PurchaseRequest request); }
(2)Client.java:
package shiyan15; public class Client { public static void main(String[] args) { Approver wjzhang, rhuang, gyang, jguo, meeting; wjzhang = new Director("张三"); rhuang = new Manager("李四"); gyang = new VicePresident("马五"); jguo = new President("李想"); meeting = new Congress("职工大会"); // 创建职责链 wjzhang.setSuccessor(rhuang); rhuang.setSuccessor(gyang); gyang.setSuccessor(jguo); jguo.setSuccessor(meeting); // 创建采购单 PurchaseRequest pr1 = new PurchaseRequest(5000, 10001, "购买A4纸十万张"); wjzhang.processRequest(pr1); PurchaseRequest pr2 = new PurchaseRequest(40000, 10002, "购买打印机十台"); wjzhang.processRequest(pr2); PurchaseRequest pr3 = new PurchaseRequest(90000, 10003, "购买咖啡机十台"); wjzhang.processRequest(pr3); PurchaseRequest pr4 = new PurchaseRequest(180000, 10004, "购买电脑一百台"); wjzhang.processRequest(pr4); PurchaseRequest pr5 = new PurchaseRequest(800000, 10005, "购买投影仪两百台"); wjzhang.processRequest(pr5); } }
(3)Congress.java:
package shiyan15; public class Congress extends Approver { public Congress(String name) { super(name); } @Override public void processRequest(PurchaseRequest request) { System.out.println("召开职工大会审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "."); } }
(4)Director.java:
package shiyan15; public class Director extends Approver { //具体处理者 public Director(String name) { super(name); } @Override public void processRequest(PurchaseRequest request) { if (request.getAmount() < 10000) { System.out.println("主任" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "."); } else { this.successor.processRequest(request);// 转发请求 } } }
(5)Manager.java:
package shiyan15; public class Manager extends Approver { public Manager(String name) { super(name); } @Override public void processRequest(PurchaseRequest request) { if (request.getAmount() < 50000) { System.out.println("部门经理" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "."); } else { this.successor.processRequest(request);// 转发请求 } } }
(6)Prisident.java:
package shiyan15; public class President extends Approver { public President(String name) { super(name); } @Override public void processRequest(PurchaseRequest request) { if (request.getAmount() < 200000) { System.out.println("总经理" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "."); } else { } } }
(7)PurchaseRequest.java:
package shiyan15; public class PurchaseRequest { //采购人员类 private double amount;// 采购金额 private int number;// 采购单编号 private String purpose;// 采购目的 public PurchaseRequest(double amount, int number, String purpose) { this.amount = amount; this.number = number; this.purpose = purpose; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getPurpose() { return purpose; } public void setPurpose(String purpose) { this.purpose = purpose; } }
(8)VicePresident.java:
package shiyan15; public class VicePresident extends Approver { public VicePresident(String name) { super(name); } @Override public void processRequest(PurchaseRequest request) { if (request.getAmount() < 100000) { System.out.println("副总经理" + this.name + "审批采购单:" + request.getNumber() + "," + "金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "."); } else { this.successor.processRequest(request);// 转发请求 } } }
(9)实现截图: