• 职责链模式


    对请求的发送者和接收者进行解耦,Servlet 的 Filter就是采用职责链设计模式

    public class Chain {
    
        List<ChainHandler> handlers = null;
        
        private int index = 0;
        
        public Chain(List<ChainHandler> handlers) {
            this.handlers = handlers;
        }
    
        public void process() {
            if(index >= handlers.size())
                return;
            handlers.get(index++).execute(this);
        }
    
    }
    public abstract class ChainHandler {
    
        public void execute(Chain chain) {
            process();
            chain.process();
        }
        
        protected abstract  void process();
        
        
    }
    public class ChainTest {
        public static void main(String[] args) {
    
            List<ChainHandler> handlers = Arrays.asList(
                       new ChainHandlerA(),
                       new ChainHandlerB(),
                       new ChainHandlerC()
                    );
            
            Chain chain = new Chain(handlers);
            chain.process();
        }
        
        static class ChainHandlerA extends ChainHandler{
            @Override
            protected void process() {
                System.out.println("handle by ChainHandlerA ");
            }
        }
        
        static class ChainHandlerB extends ChainHandler{
            @Override
            protected void process() {
                System.out.println("handle by ChainHandlerB ");
            }
        }
        
        static class ChainHandlerC extends ChainHandler{
            @Override
            protected void process() {
                System.out.println("handle by ChainHandlerC ");
            }
        }
    }
  • 相关阅读:
    spark的做算子统计的Java代码(在Linux系统集群式运行)
    http协议面试题
    vue响应式原理
    vue-cli3搭建vue项目
    vscode中自定义代码片段
    vue中常用的全局配置
    tomcat安装配置
    Git相关
    nginx配置文件详解
    nginx源码安装
  • 原文地址:https://www.cnblogs.com/moris5013/p/11068073.html
Copyright © 2020-2023  润新知