java设计模式之职责链设计模式

1.定义

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.

2.意识图

 

3.代码示例

 传递一个字符串最终被执行时需要接受拦截器栈的过滤

1).拦截器接口

package com.wyl.chain;

/**
 * 拦截器接口
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public interface Interceptor {
    /**
     * 拦截方法
     * @param str  模拟字符串
     * @param chain  拦截器栈
     */
    public void doFilter(String str,InterceptorChain chain);
}

 2).拦截器实现类A

package com.wyl.chain;

/**
 * 拦截器A
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class InterceptorA implements Interceptor {
    
    @Override
    public void doFilter(String str, InterceptorChain chain) {
        if(str.indexOf("hello")!=-1) chain.executeNextInterceptor(str);
        else RequestUtil.addParam(str, "被A拦截了");
    }

}

3).拦截器实现类B

package com.wyl.chain;

/**
 * 拦截器B
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class InterceptorB implements Interceptor {

    @Override
    public void doFilter(String str, InterceptorChain chain) {
        if(str.indexOf("world")!=-1) chain.executeNextInterceptor(str);
        else RequestUtil.addParam(str, "被B拦截了");
    }

}

4).拦截器实现类C

package com.wyl.chain;

/**
 * 拦截器C
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class InterceptorC implements Interceptor {

    @Override
    public void doFilter(String str, InterceptorChain chain) {
        if(str.indexOf("this")!=-1) chain.executeNextInterceptor(str);
        else RequestUtil.addParam(str, "被C拦截了");
    }

}

5).拦截器栈

package com.wyl.chain;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器栈
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class InterceptorChain {
    //拦截器集合
    public static List<Interceptor> chains=new ArrayList<>();
    
    //拦截器索引
    private static int index=0;
    
    //添加拦截器
    public InterceptorChain addInterceptor(List<Interceptor> interceptors ) {
        for (Interceptor interceptor : interceptors) {
            chains.add(interceptor);
        }
        return this;
    }
    
    //执行下一个拦截器
    public void executeNextInterceptor(String str) {
        if(index>chains.size()) return;
        chains.get(index++).doFilter(str, this);
    }
    
}

6).参数类

package com.wyl.chain;

/**
 * map中的key,用枚举表示
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public enum Param {
    STR,DESC
}

7).工具类

package com.wyl.chain;

import java.util.HashMap;
import java.util.Map;

/**
 * map存储拦截情况
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class RequestUtil {
    public static Map<String,String> map=new HashMap<>();
    public static void addParam(String str,String description) {
        map.put(Param.STR.toString(), str);
        map.put(Param.DESC.toString(), description);
    }
    
}

8).目标执行类

package com.wyl.chain;

/**
 * 要执行的Action
 * wuyanlin2016@163.com
 *  2017年12月27日
 */
public class Action {
    
    //执行的方法
    public void execute() {
        System.out.println("原字符串:"+RequestUtil.map.get(Param.STR.toString()));
        System.out.println("说明:"+RequestUtil.map.get(Param.DESC.toString()));
    }
}

9).客户端类

package com.wyl.chain;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户端 wuyanlin2016@163.com 2017年12月27日
 */
public class Client {
    public static void main(String[] args) {
        
        //创建拦截器栈
        InterceptorChain chain = new InterceptorChain();
        
        //拦截器集合
        List<Interceptor> list = new ArrayList<>();
        list.add(new InterceptorA());
        list.add(new InterceptorB());
        list.add(new InterceptorC());
        
        //添加拦截器
        InterceptorChain chainWithInterceptor = chain.addInterceptor(list);
        
        //执行拦截操作
        chainWithInterceptor.executeNextInterceptor("hello world");
        
        //执行目标方法
        new Action().execute();
        
    }
}

10).结果

原字符串:hello world
说明:被C拦截了

4.总结

何时选用?

1).多个对象处理一个请求

2).不明确接收者的情况下,多个对象中的一个提交一个请求的话

3).动态构建一个对象的请求集合

原文地址:https://www.cnblogs.com/wuyanlin/p/8125744.html