1. 意图:
为其他对象提供一种代理以控制对这个对象的访问

2. 别名:
surrogate替身

3. 动机
按需创建, 替代对象

4. 适用性
* 远程代理
* 虚代理
* 保护代理
* 智能指引

5. 结构


6. 实例

Java代码 复制代码
  1. package net.yeah.fanyamin.pattern.proxy;   
  2.   
  3. /**  
  4.  * @author walter  
  5.  */  
  6. interface Greet {   
  7.     void sayHello(String name);   
  8.     void goodBye();   
  9. }   
  10.   
  11. class GreetImpl implements Greet {   
  12.     public void sayHello(String name) {   
  13.         System.out.println("Hello " + name);   
  14.     }   
  15.     public void goodBye() {   
  16.         System.out.println("Good bye.");   
  17.     }   
  18. }   
  19.   
  20. public class SimpleProxy implements Greet {   
  21.     private Greet greet = null;   
  22.        
  23.     SimpleProxy(Greet greet) {   
  24.         this.greet = greet;   
  25.     }   
  26.        
  27.     public void sayHello(String name) {   
  28.         System.out.println("--before method sayHello");   
  29.         greet.sayHello(name);   
  30.         System.out.println("--after method sayHello");   
  31.     }   
  32.        
  33.     public void goodBye() {   
  34.         System.out.println("--before method goodBye");   
  35.         greet.goodBye();   
  36.         System.out.println("--after method goodBye");   
  37.     }   
  38.     /**  
  39.      * @param args  
  40.      */  
  41.     public static void main(String[] args) {   
  42.         Greet greet = new SimpleProxy(new GreetImpl());   
  43.         greet.sayHello("walter");   
  44.         greet.goodBye();   
  45.   
  46.     }   
  47.   
  48. }  

 利用JDK中的动态代理

Java代码 复制代码
  1. /**  
  2.  *   
  3.  */  
  4. package net.yeah.fanyamin.pattern.proxy;   
  5.   
  6. import java.lang.reflect.InvocationTargetException;   
  7. import java.lang.reflect.Method;   
  8.   
  9. /**  
  10.  * @author walter  
  11.  */  
  12. public class DebugProxy implements java.lang.reflect.InvocationHandler {   
  13.   
  14.     private Object obj;   
  15.   
  16.     public static Object newInstance(Object obj) {   
  17.         return java.lang.reflect.Proxy.newProxyInstance(obj.getClass().getClassLoader(),   
  18.                                                         obj.getClass().getInterfaces(), new DebugProxy(obj));   
  19.     }   
  20.   
  21.     private DebugProxy(Object obj) {   
  22.         this.obj = obj;   
  23.     }   
  24.   
  25.     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {   
  26.         Object result;   
  27.         try {   
  28.             System.out.println("--before method " + m.getName());   
  29.             result = m.invoke(obj, args);   
  30.         } catch (InvocationTargetException e) {   
  31.             throw e.getTargetException();   
  32.         } catch (Exception e) {   
  33.             throw new RuntimeException("unexpected invocation exception: " + e.getMessage());   
  34.         } finally {   
  35.             System.out.println("--after method " + m.getName());   
  36.         }   
  37.         return result;   
  38.     }   
  39.   
  40.     /**  
  41.      * @param args  
  42.      */  
  43.     public static void main(String[] args) {   
  44.         Greet greet = (Greet) DebugProxy.newInstance(new GreetImpl());   
  45.         greet.sayHello("walter");   
  46.         greet.goodBye();   
  47.     }   
  48.   
  49. }  



动态代理确实很有价值,而且java的反射机制其实性能并不慢,只不过被代理的Object需要有个Interface就是了。
实际中,代理多用在访问,权限控制
其实从类的实现表现形式来说,和装饰模式,适配器模式,都比较相似,只不过具体实现意义不一样
posted on 2008-11-04 10:20 caihaibo 阅读(103) 评论(0)  编辑  收藏 所属分类: java模式

只有注册用户登录后才能发表评论。


网站导航: