隨筆 - 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


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


          網站導航:
           
          主站蜘蛛池模板: 富宁县| 黎城县| 枞阳县| 阳西县| 娄烦县| 三穗县| 昭平县| 隆化县| 全州县| 中西区| 内黄县| 昂仁县| 成安县| 安达市| 安溪县| 齐河县| 墨脱县| 汉阴县| 黄陵县| 张家川| 凉山| 仁布县| 济源市| 四会市| 淮南市| 新闻| 攀枝花市| 彭州市| 禹州市| 屏边| 亚东县| 武夷山市| 沙河市| 宜良县| 惠安县| 玛曲县| 浑源县| 上饶县| 星座| 浮山县| 兴安盟|