隨筆 - 45, 文章 - 6, 評論 - 4, 引用 - 0
          數據加載中……

          java動態代理實例

          1.真實對象接口
          1 public interface IAnimal {
          2     void info();
          3 }

          2.真實類
          1 public class Cat implements IAnimal{
          2 
          3     public void info() {
          4         System.out.println("This is a cat!");
          5     }
          6 
          7 }

          3.調用處理器
           1 import java.lang.reflect.InvocationHandler;
           2 import java.lang.reflect.Method;
           3 
           4 public class TraceHandler implements InvocationHandler{
           5     
           6     private Object target;//以真實角色作為代理角色的屬性
           7     
           8     //構造器
           9     public TraceHandler(Object target) {   
          10         this.target = target;   
          11     }   
          12 
          13     /*
          14      * 通過反射機制動態執行真實角色的每一個方法
          15      */
          16     public Object invoke(Object proxy, Method method, Object[] args)
          17             throws Throwable {
          18         try {
          19             System.out.println("被攔截的方法:" + method.getName()); 
          20             System.out.println("預處理.");
          21             
          22             return method.invoke(target, args);//調用真是對象的method方法
          23             
          24         } catch (Exception e) {
          25             return null;
          26         } finally {
          27              System.out.println("善后處理.");
          28         }
          29     }
          30 }

          4.客戶端
           1 import java.lang.reflect.InvocationHandler;
           2 import java.lang.reflect.Proxy;
           3 
           4 public class ProxyTest {
           5     public static void main(String[] args) {
           6         
           7         //真實對象(即被代理對象)
           8         final IAnimal animal = new Cat();
           9         
          10         //為真實對象制定一個調用處理器
          11         InvocationHandler handler = new TraceHandler(animal);
          12         
          13         //獲得真實對象(animal)的一個代理類 ★★★★★
          14         Object proxyObj = Proxy.newProxyInstance(
          15                 animal.getClass().getClassLoader(), //真實對象的類加載器
          16                 animal.getClass().getInterfaces(),  //真實對象實現的所有接口
          17                 handler                                //真實對象的處理器
          18                 );
          19          
          20          if (proxyObj instanceof IAnimal) {
          21             System.out.println("the proxyObj is an animal!");
          22             
          23             IAnimal animalProxy = (IAnimal)proxyObj;//proxyObj與animal都實現了IAnimal接口
          24             
          25             animalProxy.info();//像普通animal對象一樣使用(通過handler的invoke方法執行)
          26         } else {
          27             System.out.println("the proxyObj isn't an animal!");
          28         }
          29     }
          30 }

          posted on 2009-10-24 21:44 liyang 閱讀(2603) 評論(0)  編輯  收藏 所屬分類: design pattern


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临颍县| 佛山市| 荃湾区| 分宜县| 都昌县| 柞水县| 永泰县| 黎平县| 辽宁省| 施甸县| 饶阳县| 河南省| 商南县| 巴彦县| 吉林市| 恩施市| 灵璧县| 四平市| 徐水县| 正宁县| 工布江达县| 淮安市| 清河县| 双桥区| 成武县| 朝阳市| 梧州市| 惠来县| 清流县| 香港 | 监利县| 定兴县| 永靖县| 江都市| 石嘴山市| 桑日县| 榆社县| 广水市| 永州市| 达日县| 彭山县|