• 职责链模式


    对请求的发送者和接收者进行解耦,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 ");
            }
        }
    }
  • 相关阅读:
    弹飞绵羊
    POJ 3308
    狼抓兔子
    块状链表题*1
    块状链表
    双向链表
    Linux入职基础-1.2_U盘安装RedHat5具体步骤
    Linux入职基础-1.1_国内开源的主要镜像站
    VS.NET(C#)--2.9_HTML服务器控件案例
    VS2015按钮方法
  • 原文地址:https://www.cnblogs.com/moris5013/p/11068073.html
Copyright © 2020-2023  润新知