public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //创建一个请求 PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1); //创建相关的审批人 DepartmentApprover departmentApprover = new DepartmentApprover("张主任"); CollegeApprover collegeApprover = new CollegeApprover("李院长"); ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校"); SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("佟校长"); //需要将各个审批级别的下一个设置好 (处理人构成环形: ) departmentApprover.setApprover(collegeApprover); collegeApprover.setApprover(viceSchoolMasterApprover); viceSchoolMasterApprover.setApprover(schoolMasterApprover); schoolMasterApprover.setApprover(departmentApprover); departmentApprover.processRequest(purchaseRequest); viceSchoolMasterApprover.processRequest(purchaseRequest); } } //请求类 public class PurchaseRequest { private int type = 0; //请求类型 private float price = 0.0f; //请求金额 private int id = 0; //构造器 public PurchaseRequest(int type, float price, int id) { this.type = type; this.price = price; this.id = id; } public int getType() { return type; } public float getPrice() { return price; } public int getId() { return id; } } //抽象处理类,聚合下一个处理对象 public abstract class Approver { Approver approver; //下一个处理者 String name; // 名字 public Approver(String name) { // TODO Auto-generated constructor stub this.name = name; } //下一个处理者 public void setApprover(Approver approver) { this.approver = approver; } //处理审批请求的方法,得到一个请求, 处理是子类完成,因此该方法做成抽象 public abstract void processRequest(PurchaseRequest purchaseRequest); } //处理子类 public class DepartmentApprover extends Approver { public DepartmentApprover(String name) { // TODO Auto-generated constructor stub super(name); } @Override public void processRequest(PurchaseRequest purchaseRequest) { // TODO Auto-generated method stub if(purchaseRequest.getPrice() <= 5000) { System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理"); }else {//继续执行下一个链 approver.processRequest(purchaseRequest); } } } public class CollegeApprover extends Approver { public CollegeApprover(String name) { // TODO Auto-generated constructor stub super(name); } @Override public void processRequest(PurchaseRequest purchaseRequest) { // TODO Auto-generated method stub if(purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) { System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理"); }else { approver.processRequest(purchaseRequest); } } } public class ViceSchoolMasterApprover extends Approver { public ViceSchoolMasterApprover(String name) { // TODO Auto-generated constructor stub super(name); } @Override public void processRequest(PurchaseRequest purchaseRequest) { // TODO Auto-generated method stub if(purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000) { System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理"); }else { approver.processRequest(purchaseRequest); } } } public class SchoolMasterApprover extends Approver { public SchoolMasterApprover(String name) { // TODO Auto-generated constructor stub super(name); } @Override public void processRequest(PurchaseRequest purchaseRequest) { // TODO Auto-generated method stub if(purchaseRequest.getPrice() > 30000) { System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理"); }else { approver.processRequest(purchaseRequest); } } }
public class ResponsibilityChain { public static void main(String[] args) { // TODO Auto-generated method stub // DispatcherServlet //说明 /* * * protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { * HandlerExecutionChain mappedHandler = null; * mappedHandler = getHandler(processedRequest);//获取到HandlerExecutionChain 对象 * //在 mappedHandler.applyPreHandle 内部 得到啦 HandlerInterceptor interceptor * //调用了拦截器的 interceptor.preHandle * if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } //说明:mappedHandler.applyPostHandle 方法内部获取到拦截器,并调用 //拦截器的 interceptor.postHandle(request, response, this.handler, mv); mappedHandler.applyPostHandle(processedRequest, response, mv); * } * * * //说明:在 mappedHandler.applyPreHandle 内部中, * 还调用了 triggerAfterCompletion 方法,该方法中调用了 * HandlerInterceptor interceptor = getInterceptors()[i]; try { interceptor.afterCompletion(request, response, this.handler, ex); } catch (Throwable ex2) { logger.error("HandlerInterceptor.afterCompletion threw exception", ex2); } */ } }
说明:
(1)springmvc 请求的流程图中,执行了 拦截器相关方法 interceptor.preHandler 等等
(2)在处理 SpringMvc 请求时,使用到职责链模式还使用到适配器模式
(3)HandlerExecutionChain 主要负责的是请求拦截器的执行和请求处理,但是他本身不处理请求,只是将请求分配
给链上注册处理器执行,这是职责链实现方式,减少职责链本身与处理逻辑之间的耦合,规范了处理流程
(4)HandlerExecutionChain 维护了 HandlerInterceptor 的集合, 可以向其中注册相应的拦截器.
6. 职责链模式注意事项和细节
(1)将请求和处理分开,实现解耦,提高系统的灵活性
(2)简化了对象,使对象不需要知道链的结构
(3)性能会受到影响,特别是在链比较长的时候,因此需控制链中最大节点数量,一般通过在 Handler 中设置一个
最大节点数量,在 setNext()方法中判断是否已经超过阀值,超过则不允许该链建立,避免出现超长链无意识地破坏系统性能
(4)调试不方便。采用了类似递归的方式,调试时逻辑可能比较复杂
(5)最佳应用场景:有多个对象可以处理同一个请求时,比如:多级请求、请假/加薪等审批流程、Java Web 中 Tomcat对 Encoding 的处理、拦截器