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 閱讀(110) 評論(0)  編輯  收藏 所屬分類: java模式
          主站蜘蛛池模板: 黔西县| 栾城县| 纳雍县| 昭苏县| 博湖县| 冷水江市| 周宁县| 泸定县| 天津市| 芜湖市| 恩施市| 佛学| 鄂尔多斯市| 浦县| 从江县| 关岭| 聂荣县| 鹤庆县| 信宜市| 辉县市| 青岛市| 桦甸市| 房产| 申扎县| 德安县| 乌苏市| 确山县| 温宿县| 扶沟县| 塘沽区| 武夷山市| 北京市| 兴山县| 霍城县| 抚顺市| 婺源县| 乐昌市| 贺州市| 泽库县| 巩留县| 清苑县|