Java design patterns described in (15): Chain of Responsibility pattern

This article Source: GitHub · Click here || GitEE · Click here

One, described scenes of life

1, leave approval process

The company's common leave approval process: leave days

当 day<=3   天,项目经理审批
当 3<day<=5 天,部门经理审批
当 day>5    天,CEO审批

2, the flow diagram

3, code implementation

public class C01_InScene {
    public static void main(String[] args) {
        // 组装责任链
        AuditHandler h1 = new CeoManger();
        AuditHandler h2 = new DeptManger();
        AuditHandler h3 = new ProjectManger();
        h3.setSuccessor(h2);
        h2.setSuccessor(h1);
        /*
         * 测试输出
         * 项目经理无权审批
         * 部门经理无权审批
         * CEO审批:同意【Cicada】,请假【6】天
         */
        h3.handleLeaveDay("Cicada",6);
    }
}
abstract class AuditHandler {
    //持有下一个处理请求的对象
    protected AuditHandler successor = null;
    public AuditHandler getSuccessor() {
        return successor;
    }
    public void setSuccessor(AuditHandler successor) {
        this.successor = successor;
    }
    public abstract void handleLeaveDay (String user,Integer day);
}

/**
 * 项目经理审批
 */
class ProjectManger extends AuditHandler{
    @Override
    public void handleLeaveDay(String user, Integer day) {
        if (day <= 3){
            System.out.println("项目经理审批:同意【"+user+"】,请假【"+day+"】天");
        } else {
            System.out.println("项目经理无权审批");
            if (getSuccessor() != null){
                getSuccessor().handleLeaveDay(user,day);
            }
        }
    }
}
/**
 * 部门经理审批
 */
class DeptManger extends AuditHandler{
    @Override
    public void handleLeaveDay(String user, Integer day) {
        if (day > 3 && day <= 5){
            System.out.println("部门经理审批:同意【"+user+"】,请假【"+day+"】天");
        } else {
            System.out.println("部门经理无权审批");
            if (getSuccessor() != null){
                getSuccessor().handleLeaveDay(user,day);
            }
        }
    }
}
/**
 * CEO审批
 */
class CeoManger extends AuditHandler{
    @Override
    public void handleLeaveDay(String user, Integer day) {
        if (day > 5){
            System.out.println("CEO审批:同意【"+user+"】,请假【"+day+"】天");
        } else {
            if (getSuccessor() != null){
                getSuccessor().handleLeaveDay(user,day);
            }
        }
    }
}

Second, the chain of responsibility pattern

1, the basic concept

Chain of Responsibility pattern is an object-behavior patterns of. In the chain of responsibility pattern, many objects referenced by each of the object on the next are connected together to form a chain structure. In this chain transfer request until a certain object on the right chain to process the request. Requesting client does not know which object on the chain to process the request, which allows the system to dynamically re-organize and assign responsibility objects without affecting the client.

2, the core role

(1), the role of the abstract handler

Defining processing request to an interface. Interface may be set to give a method and returns a reference to the next object. This role usually consists of a Java abstract classes or Java interface.

(2) specifically addresses the role

DETAILED Processor upon request, the request processing may be selected out, or the subject requests to the next. Due to the specific handler holds a reference to the next home.

3, illustrates a pattern

4, the source code to achieve

public class C02_Chain {
    public static void main(String[] args) {
        // 组装责任链
        Handler handler1 = new ConcreteHandler();
        Handler handler2 = new ConcreteHandler();
        handler1.setHandler(handler2);
        // 提交请求
        handler1.handlerRequest();
    }
}
/**
 * 抽象处理者角色
 */
abstract class Handler {
    /*
     * 持有后续的责任对象
     */
    protected Handler handler;
    /**
     * 处理请求的方法
     */
    public abstract void handlerRequest();
    public Handler getHandler() {
        return handler;
    }
    public void setHandler(Handler handler) {
        this.handler = handler;
    }
}
/**
 * 具体处理者角色
 */
class ConcreteHandler extends Handler{
    /**
     * 调用该方法处理请求
     */
    @Override
    public void handlerRequest() {
        /*
         * 判断是否有后续的责任对象,没有就出来请求,有就直接放过
         */
        if(getHandler() != null){
            System.out.println("放过请求,下个对象处理...");
            getHandler().handlerRequest();
        } else{
            System.out.println("直接处理请求了...");
        }
    }
}

Three, Spring Framework application

1, DispatcherServlet class

DispatcherServlet core methods doDispatch. HandlerExecutionChain only collection of HandlerInterceptor maintenance, which can be registered to the respective interceptor itself does not directly process the request, the request is assigned to the registration processor performs the chain of responsibility, to reduce the degree of coupling between itself and the processing logic duty chain.

HandlerExecutionChain mappedHandler = null;
mappedHandler = this.getHandler(processedRequest);
mappedHandler.applyPreHandle(processedRequest, response);
mappedHandler.applyPostHandle(processedRequest, response, mv);

2、HandlerExecutionChain类

Several analysis methods described herein, a method is requested from doDispatch DispatcherServlet class.

  • Gets interceptor, execute preHandle method.
boolean applyPreHandle(HttpServletRequest request, 
                       HttpServletResponse response) throws Exception {
    HandlerInterceptor[] interceptors = this.getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for(int i = 0; i < interceptors.length; this.interceptorIndex = i++) {
            HandlerInterceptor interceptor = interceptors[i];
            if (!interceptor.preHandle(request, response, this.handler)) {
                this.triggerAfterCompletion(request, response, (Exception)null);
                return false;
            }
        }
    }
    return true;
}
  • In applyPreHandle process performed triggerAfterCompletion method.
void triggerAfterCompletion(HttpServletRequest request, 
                            HttpServletResponse response, Exception ex) throws Exception {
    HandlerInterceptor[] interceptors = this.getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for(int i = this.interceptorIndex; i >= 0; --i) {
            HandlerInterceptor interceptor = interceptors[i];
            try {
                interceptor.afterCompletion(request, response, this.handler, ex);
            } catch (Throwable var8) {
                logger.error("HandlerInterceptor.afterCompletion threw exception", var8);
            }
        }
    }
}
  • Gets interceptor, execute applyPostHandle method.
void applyPostHandle(HttpServletRequest request, 
                     HttpServletResponse response, ModelAndView mv) 
                     throws Exception {
    HandlerInterceptor[] interceptors = this.getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for(int i = interceptors.length - 1; i >= 0; --i) {
            HandlerInterceptor interceptor = interceptors[i];
            interceptor.postHandle(request, response, this.handler, mv);
        }
    }
}

Fourth, the model summary

  1. Separate request, and processing logic, the flexibility to achieve improved decoupling system;
  2. When the chain of responsibility is too long, the performance will drop, testing will become complicated;
  3. Scenario: leave, pay, and other common expenses approval process;

Fifth, the source code address

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Guess you like

Origin www.cnblogs.com/cicada-smile/p/11716928.html