• 模仿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());
        }
    
    }
  • 相关阅读:
    自定义查询条件存储过程
    管道分隔符Split
    开机进入boot menu和application menu,无法开机
    [SQL]SQL语言入门级教材_SQL数据操作基础(二)
    [SQL]SQL语言入门级教材_SQL功能与特性(一)
    编程规范(三)
    编程规范(二)
    类与结构的差别
    经典命名
    [SQL]SQL Server数据表的基础知识与增查删改
  • 原文地址:https://www.cnblogs.com/daxin/p/3352334.html
Copyright © 2020-2023  润新知