zoukankan      html  css  js  c++  java
  • 模仿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());
        }
    
    }
  • 相关阅读:
    WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞
    WordPress WP-Realty插件‘listing_id’参数SQL注入漏洞
    WordPress Videowall插件‘page_id’参数跨站脚本漏洞
    MySQL备忘点(上)
    Print工具类
    用于图片缩放的工具类
    重载、重写、方法相同
    Try-Catch-Finally代码块中的return
    Fltiss项目的架构、包名的定义和类的划分
    优化版快速排序
  • 原文地址:https://www.cnblogs.com/daxin/p/3352334.html
Copyright © 2011-2022 走看看