责任链模式(原创)

前言:

下面是一个通用的任务链实现,类似于tomcat的valve,应用仅需实现并配置valve接口相关实现即可,pipeline也作为valve的实现。共分为4部分:

第一部分为管道的相关实现,仅在spring ioc配置中需要使用,用户无需扩展。

第二部分为valve相关定义,定义了一个需要应用扩展的valve接口,业务逻辑在此处实现。

第三部分为测试用例部分。

第四部分为spring配置片段。

======================================================================

1、pipeline相关实现

======================================================================

package com.wolf.design.responsibility.chain.pipeline;

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

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午11:06
 * JDK: 1.7
 */
public class BasePipeline<V> {
    private List<V> valves;

    public BasePipeline(int initSize) {
        valves = new ArrayList<V>(initSize);
    }

    public BasePipeline() {
        this(2);
    }

    /**
     * 增加阀门
     *
     * @param valve 阀门
     */
    public void addValve(V valve) {
        valves.add(valve);
    }

    /**
     * 删除任务阀门
     *
     * @param valve 任务阀门
     */
    public void removeValve(V valve) {
        valves.remove(valve);
    }

    /**
     * 返回所有阀门
     *
     * @return 管道中的阀门
     */
    public List<V> getValves() {
        return valves;
    }

    public void setValves(List<V> valves) {
        this.valves = valves;
    }
}
=======================================================================

package com.wolf.design.responsibility.chain.pipeline;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午11:18
 * JDK: 1.7
 */
public interface Pipeline<Context, E extends Exception> extends Valve<Context,E>{
    public void handle(Context context)
            throws E;
}
======================================================================

package com.wolf.design.responsibility.chain.pipeline;


import com.wolf.design.responsibility.chain.valve.SimpleValveChain;
import com.wolf.design.responsibility.chain.valve.Valve;
import com.wolf.design.responsibility.chain.valve.ValveChain;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午11:07
 * JDK: 1.7
 *
 * 支持spring IOC配置,如{@code
 * <bean id="getFromCachePipeline" class="cn.com.betasoft.dsat.pipeline.SimplePipeline">
        <property name="valves">
            <list>
                <ref bean="setIndicatorTagValve"/>
                <ref bean="setIndicatorTimeWindowValve"/>
            </list>
        </property>
  </bean>}
 */
public class SimplePipeline<Context, E extends Exception> extends BasePipeline<Valve<Context, E>> implements Pipeline<Context,E>{
    /**
     * 管道处理。
     *
     * @param context 阀门上下文.
     */
    @Override
    public void handle(Context context)
            throws E
    {
        long begin = System.currentTimeMillis();
        ValveChain<Context, E> chain = new SimpleValveChain<Context, E>(getValves());
        chain.handleNext(context);
    }

    @Override
    public void handle(Context context, ValveChain<Context, E> chain) throws E {
        handle(context);
        chain.handleNext(context);
    }
}
======================================================================

2、Valve相关实现

======================================================================

package com.wolf.design.responsibility.chain.valve;

import java.util.List;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午10:54
 * JDK: 1.7
 */
public class BaseValveChain<V>{
    protected List<V> valves = null;

    protected int index = 0;

    protected int size;

    public BaseValveChain(List<V> valves)
    {
        this.valves = valves;
        this.size = valves.size();
    }

    public BaseValveChain(List<V> valves, int index)
    {
        this(valves);
        this.index = index;
    }
}

======================================================================

package com.wolf.design.responsibility.chain.valve;

import java.util.List;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午10:55
 * JDK: 1.7
 */
public class SimpleValveChain<Context, E extends Exception> extends BaseValveChain<Valve<Context, E>> implements ValveChain<Context, E> {

    public SimpleValveChain(List<Valve<Context, E>> valves) {
        super(valves);
    }

    @Override
    public void handleNext(Context context) throws E {
        if (index < size) {
            valves.get(index++).handle(context, this);
        }
    }
}

======================================================================

package com.wolf.design.responsibility.chain.valve;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午9:18
 * JDK: 1.7
 */

/**
 * 阀门
 * <p/>
 * <p/>
 * 标记接口
 *
 * @author jiyanbin
 * @version 1.00 Aug 22, 2003 11:52:44 PM
 *          <p/>
 *          {@link com.wolf.design.responsibility.chain.pipeline.SimplePipeline#valves}中存放的是实现此接口的阀门。如
 *          {@code
 *          public class SetIndicatorTimeWindowValve<T extends Map<String, Object>> implements Valve<T, Exception> {
 *              @Override public void handle(T context, ValveChain<T, Exception> chain) throws Exception {
 *              。。。。。。。
 *              }
 *          }
 *          }
 */
public interface Valve<Context, E extends Exception> {
    /**
     * 阀门处理接口。根据context中的信息,进行处理。ValveChain用于控制
     * 是否需要执行下一个阀门.
     *
     * @param context 阀门上下文
     * @param chain   阀门控制链,控制是否需要继续执行管道中的阀门.
     */
    public abstract void handle(Context context, ValveChain<Context, E> chain)
            throws E;
}

======================================================================

package com.wolf.design.responsibility.chain.valve;

/**
 * Author: jiyanbin
 * Date: 13-11-18:上午9:43
 * JDK: 1.7
 */
public interface ValveChain<Context,E extends Exception> {
    public void handleNext(Context context) throws E;
}

======================================================================

3、测试用例

======================================================================

package com.wolf.design.responsibility.chain.test;

import com.wolf.design.responsibility.chain.valve.Valve;
import com.wolf.design.responsibility.chain.valve.ValveChain;


import java.util.Map;


/**
 * Author: jiyanbin
 * Date: 下午12:08
 * JDK: 1.7
 */
public class ExampleValve<T extends Map<String, Object>> implements Valve<T, Exception> {

    @Override
    public void handle(T context, ValveChain<T, Exception> chain) throws Exception {
        System.out.print("abc");
        chain.handleNext(context);
    }
}

======================================================================

package com.wolf.design.responsibility.chain.test;

import com.wolf.design.responsibility.chain.pipeline.Pipeline;

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

/**
 * Author: [email protected]
 * Date: 上午10:35
 * JDK: 1.7
 */
public class PipelineMain {
    /**
     * 可以通过spring的方式获取pipeline
     */
    private Pipeline<? super Map<String, Object>, Exception> pipeline;

    public void main(String[] args) throws Exception {
        pipeline.handle(new HashMap<String, Object>());
    }

    public void setPipeline(Pipeline<? super Map<String, Object>, Exception> pipeline) {
        this.pipeline = pipeline;
    }
}

/**
 * $Log: Create By jiyanbin At 上午10:35
 * **************************************************Modify history*************************************************
 * Date                Modifier               Cause
 *
 * *****************************************************************************************************************
 */

======================================================================

4、spring配置

======================================================================

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--<import resource="dsat-se-model-result.xml"/>-->
    <!--++++++++++++++Get result from cache valve++++++++++++++-->
    <bean id="exampleValve" class="com.wolf.design.responsibility.chain.test.ExampleValve">

    </bean>

    <bean id="otherPipeline" class="com.wolf.design.responsibility.chain.pipeline.SimplePipeline">

        <property name="valves">
            <list>
                <!--如果有多个valve链,在此处配置-->
                <ref bean="exampleValve"/>
            </list>
        </property>
    </bean>

    <!--++++++++++++++Get result from cache pipeline++++++++++++++-->
    <bean id="examplePipeline" class="com.wolf.design.responsibility.chain.pipeline.SimplePipeline">
        <property name="valves">
            <list>
                <!--如果有多个valve链,在此处配置-->
                <ref bean="exampleValve"/>

                 <ref bean="otherPipeline"/>
            </list>
        </property>
    </bean>

    <bean id="pipelineMain" class="com.wolf.design.responsibility.chain.test.PipelineMain">
        <property name="pipeline" ref="examplePipeline"/>
    </bean>
</beans>

======================================================================

猜你喜欢

转载自jybzjf.iteye.com/blog/2030817