責(zé)任鏈模式(Chain of Responsibility Pattern)

責(zé)任鏈模式是一種對(duì)象的行為模式。在責(zé)任鏈模式里,很多對(duì)象由每一個(gè)對(duì)象對(duì)其下家的引用而連接起來形成一條鏈。請(qǐng)求在這個(gè)鏈上傳遞,直到鏈上的某一個(gè)對(duì)象決定處理此請(qǐng)求。發(fā)出這個(gè)請(qǐng)求的客戶端并不知道鏈上的哪一個(gè)對(duì)象最終處理這個(gè)請(qǐng)求,這使得系統(tǒng)可以在不影響客戶端的情況下動(dòng)態(tài)地重新組織和分配責(zé)任。 -《JAVA與模式》

1. 什么是責(zé)任鏈

在《HeadFisrt設(shè)計(jì)模式》一書中并沒有關(guān)于責(zé)任鏈描述,再次驗(yàn)證設(shè)計(jì)模式只是編碼的約定,某個(gè)通俗易懂的名稱,并不是某種蓋棺定論的東西。責(zé)任鏈模式和觀察者模式有相似的的地方,即事件(或責(zé)任)的傳遞。觀察者是發(fā)散式的,一對(duì)多;責(zé)任鏈?zhǔn)擎準(zhǔn)降模粋饕弧?/p>

/* 抽象的責(zé)任處理者 */

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;
    }
}
/* 具體的責(zé)任處理者 */

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);
    }
}
/* 當(dāng)有大事要發(fā)生的時(shí)候 */

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. 為什么要用責(zé)任鏈

解耦:模塊間解耦,各模塊負(fù)責(zé)各自的業(yè)務(wù),職責(zé)明確單一
有序:模塊有序排布、執(zhí)行,方便跳轉(zhuǎn)
可擴(kuò):易于擴(kuò)展,項(xiàng)鏈表一樣快速插入

3. 怎么使用責(zé)任鏈

責(zé)任鏈在使用方式上,按控制邏輯的位置可以分為:內(nèi)控和外控。

3.1 內(nèi)控

內(nèi)控是指控制邏輯在模塊內(nèi)部,由各模塊來控制責(zé)任鏈的走向:能不能繼續(xù)走下去。代表有:Tomcat

Tomcat 中對(duì) Servlet Filter 的處理
Tomcat
Tomcat
3.2 外控

外控是指控制邏輯在模塊外部,各模塊只負(fù)責(zé)各自的業(yè)務(wù),模塊跳轉(zhuǎn)邏輯單獨(dú)處理。代表有: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. 怎么選用責(zé)任鏈實(shí)現(xiàn)

洋蔥式

代表:Spring MVC Interceptor
優(yōu)點(diǎn):Interceptor 在方法界別分隔預(yù)處理、后置處理邏輯,職責(zé)明確
缺點(diǎn):大部分 Interceptor 只包含預(yù)處理或只包含后置處理,Interceptor 相對(duì)較重

半開式

代表:Zuul Filter
優(yōu)點(diǎn):輕量靈活,符合大部分業(yè)務(wù)處理邏輯(相對(duì)洋蔥式)
缺點(diǎn):不方便實(shí)現(xiàn)環(huán)裝邏輯(相對(duì)洋蔥式)

鏈表式

代表:Pigeon Filter 和 Servlet Filter
優(yōu)點(diǎn):支持環(huán)裝結(jié)構(gòu),可以實(shí)現(xiàn) try-catch 邏輯,控制更加靈活
缺點(diǎn):如果需要開放給外部使用,出錯(cuò)風(fēng)險(xiǎn)相對(duì)較高

一句話總結(jié):

  • 開放給外部業(yè)務(wù)使用,推薦洋蔥式
  • 內(nèi)部業(yè)務(wù)使用,推薦鏈表式
  • 復(fù)雜的 Servlet 的邏輯,推薦半開式
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容