管道过滤模式Demo
1.code
/** * <p> * 管道接口 * </p> * * @author liangliang.xu * @since 2022/8/12 11:27 AM */ public interface Pipeline<T> { /** * 设计基础过滤阀门,一个管道必须基本有一个基本阀门 * @param valve * @return */ boolean setBases(AbstractValve<T> valve); /** * 添加过滤阀门信息 * @param valve * @return */ boolean addValve(AbstractValve<T> valve); /** * 删除管道中的阀门 * @param valve * @return */ boolean removeValve(AbstractValve<T> valve); /** * 调用管道处理数据 */ void invoke(); }
/** * <p> * 抽象过滤阀门 * </p> * * @author liangliang.xu * @since 2022/8/12 11:38 AM */ public abstract class AbstractValve<T> { /** * 当前过滤阀门处理逻辑 * @param context 上下文 */ abstract void execute(PipelineContext<T> context); /** * 调用阀门 * @param context 上下文 */ void invoke(PipelineContext<T> context) { execute(context); context.invokeNext(); } }
/** * <p> * 管道上下文 * </p> * * @author liangliang.xu * @since 2022/8/12 11:38 AM */ public interface PipelineContext<T> { /** * 调用下一个过滤阀门 */ void invokeNext(); /** * 获取上下文数据 * @return 上下文数据 */ T getContextData(); /** * 流程中断 * @param interruptFlag 中断标识 */ void isInterruptProcess(boolean interruptFlag); }
/** * <p> * 标准数据流管道 * </p> * * @author liangliang.xu * @since 2022/8/12 11:27 AM */ public class StandardPipeline implements Pipeline<String> { /** * 存储阀门集合 */ private final List<AbstractValve<String>> valves = new ArrayList<>(); /** * 基础阀门 */ private AbstractValve<String> bases; /** 管道流转的上下文,可根据业务需要在上下文中加入流转的数据 */ private final PipelineContext<String> context; public StandardPipeline() { //模拟上下文数据初始化 init() context = new StandardPipelineContext("上下文数据"); } @Override public boolean setBases(AbstractValve<String> valve) { bases = valve; return true; } @Override public boolean addValve(AbstractValve<String> valve) { return valves.add(valve); } @Override public boolean removeValve(AbstractValve<String> valve) { if (valves.contains(valve)) { return valves.remove(valve); } return true; } @Override public void invoke() { context.invokeNext(); } /** * 定义一个管道上下文内部类 实现管道上下文接口 */ private class StandardPipelineContext implements PipelineContext<String> { LongAdder index = new LongAdder(); private final String contextData; private boolean interruptFlag; public StandardPipelineContext(String contextData){ this.contextData = contextData; } public String getContextData() { return contextData; } public void isInterruptProcess(boolean interruptFlag) { this.interruptFlag = interruptFlag; } @Override public void invokeNext() { if(interruptFlag){ return; } int intValue = index.intValue(); if (!valves.isEmpty() && intValue < valves.size()) { index.increment(); valves.get(intValue).invoke(context); } else { bases.execute(context); } } } }
/** * <p> * 基础的阀门 * </p> * * @author liangliang.xu * @since 2022/8/12 11:38 AM */ @Slf4j public class BaseValve extends AbstractValve<String> { @Override void execute(PipelineContext<String> context) { System.out.println("BaseValve > invoke > "+ context.getContextData()); } }
/** * <p> * 过滤1 * </p> * * @author liangliang.xu * @since 2022/8/12 11:39 AM */ @Slf4j public class Valve1 extends AbstractValve<String> { @Override void execute(PipelineContext<String> context) { System.out.println("Valve1 > invoke > "+ context.getContextData()); } }
/** * <p> * 过滤2 * </p> * * @author liangliang.xu * @since 2022/8/12 11:39 AM */ @Slf4j public class Valve2 extends AbstractValve<String> { @Override void execute(PipelineContext<String> context) { //中断流程 context.isInterruptProcess(true); System.out.println("Valve2 > invoke > "+ context.getContextData()); } }
public class PipelineTest { public static void main(String[] args) { StandardPipeline pipeline = new StandardPipeline(); BaseValve baseValve = new BaseValve(); Valve1 valve1 = new Valve1(); Valve2 valve2 = new Valve2(); pipeline.setBases(baseValve); //顺序添加,顺序执行 pipeline.addValve(valve1); pipeline.addValve(valve2); pipeline.invoke(); } }
美梦成真,变为事实。