天天看点

设计模式(十)_责任链模式

概述

责任链模式为某个请求创建一个对象链,每个对象依次检查此请求,并对其进行处理,或者将它传给链中的下一个对象

UML类图

设计模式(十)_责任链模式

代码实现

handle类

package com.zhb.proxy_demo.chain;

/**
 * @author: curry
 * @Date: 2018/7/30
 */
public abstract class Handler {

    private Handler sucessor;

    public Handler getSucessor() {
        return sucessor;
    }

    public void setSucessor(Handler sucessor) {
        this.sucessor = sucessor;
    }

    public void execute(){
        handleProcess();
        if(sucessor != null){
            sucessor.execute();
        }
    }

    /**
     * 交给子类实现
     */
    protected abstract void handleProcess();
}
           

Client类

package com.zhb.proxy_demo.chain;

/**
 * @author: curry
 * @Date: 2018/7/30
 */
public class Client {

    static class HandleA extends Handler {

        @Override
        protected void handleProcess() {
            System.out.println("handle by A");
        }
    }

    static class HandleB extends Handler {

        @Override
        protected void handleProcess() {
            System.out.println("handle by B");
        }
    }

    static class HandleC extends Handler {

        @Override
        protected void handleProcess() {
            System.out.println("handle by C");
        }
    }

    public static void main(String[] args) {
        Handler handlerA = new HandleA();
        Handler handlerB = new HandleB();
        Handler handlerC = new HandleC();

        handlerA.setSucessor(handlerB);
        handlerB.setSucessor(handlerC);

        handlerA.execute();
    }
}
           

执行结果

handle by A
handle by B
handle by C
           

代码优化

由于在client类中调用时,都需要设置从属关系。现在就把链式关系的代码进行封装

Chain类 (封装调用关系)

package com.zhb.proxy_demo.chain;

import java.util.List;

/**
 * @author: curry
 * @Date: 2018/7/30
 */
public class Chain {
    private List<ChainHandler> handlers;

    private int index = 0;
    public Chain(List<ChainHandler> handlers) {
        this.handlers = handlers;
    }

    public void proceed(){
        if(index >= handlers.size()){
            return;
        }
        handlers.get(index++).execute(this);
    }
}

           

ChainHandler类

package com.zhb.proxy_demo.chain;

/**
 * @author: curry
 * @Date: 2018/7/30
 */
public abstract class ChainHandler {

    public void  execute(Chain chain){
        handleProcess();
        chain.proceed();
    }

    /**
     * 交给子类实现
     */
    protected abstract void handleProcess();
}
           

ChainClient类

package com.zhb.proxy_demo.chain;

import java.util.Arrays;
import java.util.List;

/**
 * @author: curry
 * @Date: 2018/7/30
 */
public class ChainClient {
    static class ChainHandlerA extends ChainHandler{
        @Override
        protected void handleProcess() {
            System.out.println("handler by A");
        }
    }
    static class ChainHandlerB extends ChainHandler{
        @Override
        protected void handleProcess() {
            System.out.println("handler by B");
        }
    }
    static class ChainHandlerC extends ChainHandler{
        @Override
        protected void handleProcess() {
            System.out.println("handler by C");
        }
    }

    public static void main(String[] args) {
        List<ChainHandler> handlers = Arrays.asList(
                new ChainHandlerA(),
                new ChainHandlerB(),
                new ChainHandlerC()
        );

        Chain chain = new Chain(handlers);
        chain.proceed();
    }
}
           

测试结果

handler by A
handler by B
handler by C
           

总结

其实多个aop作用于同一个对象的时候,就是利用了责任链模式。如同优化后的代码

ReflectiveMethodInvocation 类的代码

@Nullable
    public Object proceed() throws Throwable {
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return this.invokeJoinpoint();
        } else {
            Object interceptorOrInterceptionAdvice = this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
            if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
                InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;
                return dm.methodMatcher.matches(this.method, this.targetClass, this.arguments) ? dm.interceptor.invoke(this) : this.proceed();
            } else {
                return ((MethodInterceptor)interceptorOrInterceptionAdvice).invoke(this);
            }
        }
    }
               

责任链模式将常用于过滤器,拦截器,事件(鼠标键盘事件,冒泡事件等)等场景

优点

  • 请求者和接收者解耦
  • 可以动态的增加或减少责任链上的对象,或者修改顺序

缺点

  • 调用者不知道请求可能被哪些责任链对象处理,不利于排错
  • 用户请求可能被责任链中途拦截,最终未必被真正执行,这点既是优点也是缺点,我们可以利用它做权限控制拦截器

学习不是要么0分,要么100分的。80分是收获;60分是收获;20分也是收获。有收获最重要。但是因为着眼于自己的不完美,最终放弃了,那就是彻底的0分了。

继续阅读