• 模仿Struts2的Interceptor拦截器实现


    模仿Struts2的Interceptor拦截器实现

    public interface Invocation {
    
        public Object invoke();
        
    }
    public interface Interceptor {
    
        public Object intercept(Invocation invocation);
        
    }
    public class Target {
    
        public Object execute() {
            System.out.println("Targer.execute()");
            return "Targer-execute";
        }
    }
    public class InvocationChain implements Invocation {
    
        private Iterator<Interceptor> iterator = null;
        
        private Target target = new Target();
        
        public void init(List<Interceptor> interceptors) {
            List<Interceptor> interceptorsList = new ArrayList<Interceptor>(interceptors);
            this.iterator = interceptorsList.iterator();
        }
        
        public Object invoke() {
            if(iterator.hasNext()) {
                return iterator.next().intercept(this);
            } else {
                return target.execute();
            }
        }
    
    }
    public class IntOne implements Interceptor {
    
        public Object intercept(Invocation invocation) {
            Object result = null;
            System.out.println("IntOne.intercept()-begin");
            result = invocation.invoke();
            System.out.println("IntOne.intercept()-end");
            return result;
        }
    
    }
    public class IntTwo implements Interceptor {
    
        public Object intercept(Invocation invocation) {
            Object result = null;
            System.out.println("IntTwo.intercept()-begin");
            result = invocation.invoke();
            System.out.println("IntTwo.intercept()-end");
            return result;
        }
    
    }
    public class Demo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            List<Interceptor> interceptors = new ArrayList<Interceptor>();
            interceptors.add(new IntOne());
            interceptors.add(new IntTwo());
            InvocationChain chain = new InvocationChain();
            chain.init(interceptors);
            System.out.println(chain.invoke());
        }
    
    }
  • 相关阅读:
    STM32 时钟配置分析
    STM32 开发板资源梳理
    STM32 摄像头实验OV2640
    STM32 TFT液晶屏与FSMC
    STM32 开发板电源与供电方式
    视觉里程计07 Qt的一些bug修改记录
    解决wireshark检测不到网卡的问题
    gdb 脚本调试
    [转] GCC 中的编译器堆栈保护技术
    使用gdbserver远程调试
  • 原文地址:https://www.cnblogs.com/daxin/p/3352334.html
Copyright © 2020-2023  润新知