Java设计模式之责任链设计模式
职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将所有处理对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
请求传递方向:从低级到高级
重点:如何形成这个责任链
特点:发出处理请求的客户端并不知道是系统当中的哪一个对象最终处理这个请求,这样系统的更改可以在不影响客户端的情况下动态地重新组织和分配责任。
1. 派生类中都具备的功能
(1) 都要设置后继者(一样)
(2) 都能实现自己职责范围内的功能(审批功能,具体审批细节不一样)
(3) 都具备把自己不能处理的请求传递给上级部门的功能(一样)
2.不同的功能
具体的处理情况不一样
3.例子以及代码实现
题目:已知某企业的采购审批是分级进行的,即根据采购金额的不同由不同层次的主管人员来审批,主任可以审批5万元以下(不包括5万元)的采购单,副董事长可以审批5 万元至10 万元(不包括10 万元)的采购单,董事长可以审批10 万元至50 万元(不包括50 万元)的采购单,50万元及以上的采购单就需要开会讨论决定。采用责任链设计模式(Chain of Responsibility)。
3.1、请求类PurchaseRequest,采购请求的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 public class PurchaseRequest { 4 5 private double amount; // 一个采购的金额 6 private int number; // 采购单编号 7 private String purpose; // 采购目的 8 9 public double getAmount() { 10 return amount; 11 } 12 public void setAmount(double amount) { 13 this.amount = amount; 14 } 15 public int getNumber() { 16 return number; 17 } 18 public void setNumber(int number) { 19 this.number = number; 20 } 21 public String getPurpose() { 22 return purpose; 23 } 24 public void setPurpose(String purpose) { 25 this.purpose = purpose; 26 } 27 28 29 }
3.2、审批者的共同父类Approver
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 public class Approver { 4 5 private Approver successor; // 上级 6 7 /** 8 * 采购单审批 9 */ 10 public void processRequest(PurchaseRequest request){ 11 if (successor!=null) { 12 successor.processRequest(request); 13 } 14 } 15 16 /** 17 * 设置上级 18 * @param successor 19 */ 20 public void setSuccessor(Approver successor){ 21 this.successor=successor; 22 } 23 }
3.3、主任审批子类Director
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 /** 4 * 主任审批 5 */ 6 public class Director extends Approver { 7 8 public void processRequest(PurchaseRequest request){ 9 if(request.getAmount()<50000){ 10 System.out.println("主任审批了"); 11 }else { 12 super.processRequest(request); 13 } 14 } 15 }
3.4、副董事长审批子类VicePresident
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 /** 4 * 副董事长审批 5 */ 6 public class VicePresident extends Approver { 7 8 public void processRequest(PurchaseRequest request){ 9 if(request.getAmount()>=50000&&request.getAmount()<100000){ 10 System.out.println("副董事长审批审批了"); 11 }else { 12 super.processRequest(request); 13 } 14 } 15 }
3.5、董事长审批子类President
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 /** 4 * 董事长审批 5 */ 6 public class President extends Approver { 7 8 public void processRequest(PurchaseRequest request){ 9 if(request.getAmount()>=100000&&request.getAmount()<500000){ 10 System.out.println("董事长审批审批了"); 11 }else { 12 super.processRequest(request); 13 } 14 } 15 }
3.6、例会审批子类Congress
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 /** 4 * 例会审批 5 */ 6 public class Congress extends Approver { 7 8 public void processRequest(PurchaseRequest request){ 9 if(request.getAmount()>=500000){ 10 System.out.println("例会审批审批了"); 11 }else { 12 super.processRequest(request); 13 } 14 } 15 }
3.7、测试类ApproverTest
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.designmode.chainofresponsibility; 2 3 import java.util.Scanner; 4 5 public class ApproverTest { 6 7 @SuppressWarnings("resource") 8 public static void main(String[] args) { 9 Congress meeting = new Congress(); 10 President president = new President(); 11 VicePresident vicePresident = new VicePresident(); 12 Director director = new Director(); 13 14 // 构造责任链 15 director.setSuccessor(vicePresident); 16 vicePresident.setSuccessor(president); 17 president.setSuccessor(meeting); 18 19 Scanner reader = new Scanner(System.in); 20 PurchaseRequest request = new PurchaseRequest(); 21 request.setAmount(reader.nextInt()); 22 director.processRequest(request); // 开始审批 23 } 24 }