Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          代理類,可以代理任何接口封裝的類
           1package com.lucky.proxy.util;
           2
           3import java.lang.reflect.InvocationHandler;
           4import java.lang.reflect.Method;
           5import java.lang.reflect.Proxy;
           6
           7public class ProxyMode implements InvocationHandler {
           8    private static ProxyMode instance = null
           9    
          10    private Object delegete;
          11    
          12    private ProxyMode(){
          13    }
           
          14    
          15    public static ProxyMode getInstance(){
          16        if (null == instance) {
          17            instance = new ProxyMode();
          18        }

          19        return instance;
          20    }

          21    
          22    public Object bind(Object delegete){
          23        this.delegete = delegete;
          24        return Proxy.newProxyInstance(delegete.getClass().getClassLoader(), delegete.getClass().getInterfaces(), this);
          25    }

          26    
          27    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          28        // TODO Auto-generated method stub
          29        System.out.println("調用了InvoactionHandler的invoke方法..");
          30        Object result = method.invoke(delegete, args);
          31        return result;
          32    }

          33
          34}

          35
          接口
          1package com.lucky.proxy;
          2
          3public interface IProxyClass {
          4    public String sayHello(String str);
          5    public void print();
          6    public String getInnerStr();
          7}

          8
          接口實現類
           1package com.lucky.proxy;
           2
           3public class ProxyClass implements IProxyClass {
           4    private String innerStr;
           5    public ProxyClass(){}
           6    public ProxyClass(String str){
           7        innerStr = str;
           8    }

           9    public String getInnerStr() {
          10        return innerStr;
          11    }

          12    public void setInnerStr(String innerStr) {
          13        this.innerStr = innerStr;
          14    }

          15    public void print() {
          16        // TODO Auto-generated method stub
          17        System.out.println("調用代理實現類打印信息");
          18    }

          19
          20    public String sayHello(String str) {
          21        // TODO Auto-generated method stub
          22        System.out.println(str+" 你好!");
          23        return str+" 你好!";
          24    }

          25    
          26}

          27
          測試類
           1package com.lucky.proxy.test;
           2
           3import com.lucky.proxy.IProxyClass;
           4import com.lucky.proxy.IProxyClass2;
           5import com.lucky.proxy.ProxyClass;
           6import com.lucky.proxy.ProxyClass2;
           7import com.lucky.proxy.util.ProxyMode;
           8
           9public class ProxyTest {
          10
          11    /**
          12     * @param args
          13     */

          14    public static void main(String[] args) {
          15        // TODO Auto-generated method stub
          16        IProxyClass proxyInterface = (IProxyClass) ProxyMode.getInstance().bind(new ProxyClass("構造"));
          17        proxyInterface.sayHello("李彬");
          18        proxyInterface.print();
          19        System.out.println(proxyInterface.getInnerStr());
          20        
          21        
          22        
          23//        IProxyClass2 proxyInterface = (IProxyClass2) ProxyMode.getInstance().bind(new ProxyClass2());
          24//        proxyInterface.sayHello("李彬");
          25//        proxyInterface.print();
          26        
          27    }

          28
          29}

          30
          用反射實現代理模式
           1package com.lucky.proxy.util;
           2
           3public class ProxyReflectMode {
           4    private static ProxyReflectMode instance = null;
           5    
           6    private ProxyReflectMode(){}
           7    
           8    public static ProxyReflectMode getInstance(){
           9        if (null == instance) {
          10            instance = new ProxyReflectMode();
          11        }

          12        return instance;
          13    }

          14    
          15    public Object getProxyObject(Class cls){
          16        String className = cls.getName();
          17        Object obj = null;
          18        try {
          19            obj = Class.forName(className).newInstance();
          20        }
           catch (InstantiationException e) {
          21            // TODO Auto-generated catch block
          22            e.printStackTrace();
          23        }
           catch (IllegalAccessException e) {
          24            // TODO Auto-generated catch block
          25            e.printStackTrace();
          26        }
           catch (ClassNotFoundException e) {
          27            // TODO Auto-generated catch block
          28            e.printStackTrace();
          29        }

          30        return obj;
          31    }

          32}

          33
          posted on 2007-09-22 21:59 禮物 閱讀(389) 評論(1)  編輯  收藏

          評論

          # re: 代理模式 2008-01-04 14:23 yaSong
          很有層次的寫法,現在也很流行的寫法!  回復  更多評論
            


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

          網站導航:
           
          主站蜘蛛池模板: 娄底市| 洪湖市| 宽甸| 望江县| 南江县| 商水县| 博客| 金坛市| 平塘县| 任丘市| 淮南市| 安西县| 博客| 辉南县| 固阳县| 神池县| 友谊县| 九台市| 朝阳县| 葫芦岛市| 沈丘县| 新野县| 尚志市| 西宁市| 得荣县| 衡阳市| 调兵山市| 梁山县| 股票| 龙口市| 宝坻区| 北碚区| 蛟河市| 民勤县| 封丘县| 蓝田县| 河曲县| 松江区| 灵川县| 遂宁市| 荔波县|