责任链模式(Chain of Responsibility Pattern)

责任链模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。 -《JAVA与模式》

1. 什么是责任链

在《HeadFisrt设计模式》一书中并没有关于责任链描述,再次验证设计模式只是编码的约定,某个通俗易懂的名称,并不是某种盖棺定论的东西。责任链模式和观察者模式有相似的的地方,即事件(或责任)的传递。观察者是发散式的,一对多;责任链是链式的,一传一。

/* 抽象的责任处理者 */

public abstract class AbstractHandler {

    private AbstractHandler next;

    public void handle(String param) {
        /* 做自己的事情 让别人无事可做 */
        customized(param);

        if (next != null) {
            next.handle(param);
        }
    }

    public abstract void customized(String param);

    public void setNext(AbstractHandler next) {
        this.next = next;
    }
}
/* 具体的责任处理者 */

public class Handler1 extends AbstractHandler {

    @Override
    public void customized(String param) {
        System.out.println("handler1: handle " + param);
    }
}

public class Handler2 extends AbstractHandler {

    @Override
    public void customized(String param) {
        System.out.println("handler2: handle " + param);
    }
}

public class Handler3 extends AbstractHandler {

    @Override
    public void customized(String param) {
        System.out.println("handler3: handle " + param);
    }
}
/* 当有大事要发生的时候 */

public class HandlerMain {

    public static void main(String[] args) {
        AbstractHandler chain = HandlerChainFactory.generate();
        chain.handle("rabbit");
    }
}

public class HandlerChainFactory {

    public static AbstractHandler generate() {
        AbstractHandler chain = new Handler1();
        Handler2 handler2 = new Handler2();
        Handler3 handler3 = new Handler3();

        chain.setNext(handler2);
        handler2.setNext(handler3);

        return chain;
    }
}

2. 为什么要用责任链

解耦:模块间解耦,各模块负责各自的业务,职责明确单一
有序:模块有序排布、执行,方便跳转
可扩:易于扩展,项链表一样快速插入

3. 怎么使用责任链

责任链在使用方式上,按控制逻辑的位置可以分为:内控和外控。

3.1 内控

内控是指控制逻辑在模块内部,由各模块来控制责任链的走向:能不能继续走下去。代表有:Tomcat

Tomcat 中对 Servlet Filter 的处理
Tomcat
Tomcat
3.2 外控

外控是指控制逻辑在模块外部,各模块只负责各自的业务,模块跳转逻辑单独处理。代表有:Spring MVC Interceptor 和 Zuul

  1. Spring MVC

![(https://upload-images.jianshu.io/upload_images/3596970-162b94119bbeae23.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

洋葱圈模型
DispatcherServlet
HandlerExecutionChain
  1. Zuul
/*
 * Copyright 2013 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */
package com.netflix.zuul.http;

import com.netflix.zuul.FilterProcessor;
import com.netflix.zuul.ZuulRunner;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
 * Core Zuul servlet which intializes and orchestrates zuulFilter execution
 *
 * @author Mikey Cohen
 *         Date: 12/23/11
 *         Time: 10:44 AM
 */
public class ZuulServlet extends HttpServlet {

    private static final long serialVersionUID = -3374242278843351500L;
    private ZuulRunner zuulRunner;


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        String bufferReqsStr = config.getInitParameter("buffer-requests");
        boolean bufferReqs = bufferReqsStr != null && bufferReqsStr.equals("true") ? true : false;

        zuulRunner = new ZuulRunner(bufferReqs);
    }

    @Override
    public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
        try {
            init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);

            // Marks this request as having passed through the "Zuul engine", as opposed to servlets
            // explicitly bound in web.xml, for which requests will not have the same data attached
            RequestContext context = RequestContext.getCurrentContext();
            context.setZuulEngineRan();

            try {
                preRoute();
            } catch (ZuulException e) {
                error(e);
                postRoute();
                return;
            }
            try {
                route();
            } catch (ZuulException e) {
                error(e);
                postRoute();
                return;
            }
            try {
                postRoute();
            } catch (ZuulException e) {
                error(e);
                return;
            }

        } catch (Throwable e) {
            error(new ZuulException(e, 500, "UNHANDLED_EXCEPTION_" + e.getClass().getName()));
        } finally {
            RequestContext.getCurrentContext().unset();
        }
    }

    /**
     * executes "post" ZuulFilters
     *
     * @throws ZuulException
     */
    void postRoute() throws ZuulException {
        zuulRunner.postRoute();
    }

    /**
     * executes "route" filters
     *
     * @throws ZuulException
     */
    void route() throws ZuulException {
        zuulRunner.route();
    }

    /**
     * executes "pre" filters
     *
     * @throws ZuulException
     */
    void preRoute() throws ZuulException {
        zuulRunner.preRoute();
    }

    /**
     * initializes request
     *
     * @param servletRequest
     * @param servletResponse
     */
    void init(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        zuulRunner.init(servletRequest, servletResponse);
    }

    /**
     * sets error context info and executes "error" filters
     *
     * @param e
     */
    void error(ZuulException e) {
        RequestContext.getCurrentContext().setThrowable(e);
        zuulRunner.error();
    }

}

4. 怎么选用责任链实现

洋葱式

代表:Spring MVC Interceptor
优点:Interceptor 在方法界别分隔预处理、后置处理逻辑,职责明确
缺点:大部分 Interceptor 只包含预处理或只包含后置处理,Interceptor 相对较重

半开式

代表:Zuul Filter
优点:轻量灵活,符合大部分业务处理逻辑(相对洋葱式)
缺点:不方便实现环装逻辑(相对洋葱式)

链表式

代表:Pigeon Filter 和 Servlet Filter
优点:支持环装结构,可以实现 try-catch 逻辑,控制更加灵活
缺点:如果需要开放给外部使用,出错风险相对较高

一句话总结:

  • 开放给外部业务使用,推荐洋葱式
  • 内部业务使用,推荐链表式
  • 复杂的 Servlet 的逻辑,推荐半开式

推荐阅读更多精彩内容