署理模式Proxy——读书笔记

代理模式Proxy——读书笔记
静态代理中,实现一个代理类,组合并继承被代理类的接口,接口中的方法通过调用被代理来的方法来完成。

动态代理模板:

   1. public class IJDKProxyImpl implements InvocationHandler { 
   2.     private Object target = null;     
   3.      
   4.     private IJDKProxyImpl (Object target) {     
   5.         this.target = target;     
   6.     }   
   7.      
   8.     public Object invoke(Object proxy, Method method, Object[] args) 
   9.             throws Throwable { 
  10.         System.out.println("do something before method....."); 
  11.         Object obj = method.invoke(target, args); 
  12.         System.out.println("do something after method....."); 
  13.         return obj; 
  14.     } 
  15.      
  16.     public static Object getProxy(Object target) {     
  17.         InvocationHandler handler = new IJDKProxyImpl(target);     
  18.     
  19.         Object proxy = Proxy.newProxyInstance(target.getClass()     
  20.                 .getClassLoader(), target.getClass().getInterfaces(), handler);     
  21.         return proxy;     
  22.     }   
  23.      
  24. } 


参考:http://caterpillar.onlyfun.net/Gossip/DesignPattern/ProxyPattern.htm
http://lizwjiang.iteye.com/blog/86389