1. 意圖:
          為其他對象提供一種代理以控制對這個(gè)對象的訪問

          2. 別名:
          surrogate替身

          3. 動(dòng)機(jī)
          按需創(chuàng)建, 替代對象

          4. 適用性
          * 遠(yuǎn)程代理
          * 虛代理
          * 保護(hù)代理
          * 智能指引

          5. 結(jié)構(gòu)


          6. 實(shí)例

          Java代碼 復(fù)制代碼
          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中的動(dòng)態(tài)代理

          Java代碼 復(fù)制代碼
          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. }  



          動(dòng)態(tài)代理確實(shí)很有價(jià)值,而且java的反射機(jī)制其實(shí)性能并不慢,只不過被代理的Object需要有個(gè)Interface就是了。
          實(shí)際中,代理多用在訪問,權(quán)限控制
          其實(shí)從類的實(shí)現(xiàn)表現(xiàn)形式來說,和裝飾模式,適配器模式,都比較相似,只不過具體實(shí)現(xiàn)意義不一樣
          posted on 2008-11-04 10:20 caihaibo 閱讀(110) 評論(0)  編輯  收藏 所屬分類: java模式
          主站蜘蛛池模板: 宁德市| 朔州市| 宁城县| 桂东县| 马龙县| 宁晋县| 中方县| 麟游县| 辽中县| 江都市| 平阴县| 泰宁县| 寿光市| 东平县| 宁波市| 依兰县| 科技| 呼图壁县| 兰州市| 西平县| 乐平市| 阿荣旗| 慈利县| 东乌珠穆沁旗| 冷水江市| 七台河市| 河东区| 永兴县| 关岭| 三门县| 偃师市| 浦江县| 全南县| 东兰县| 中江县| 于田县| 花莲市| 梨树县| 东海县| 军事| 黔东|