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 礼物 阅读(379) 评论(1)  编辑  收藏

评论

# re: 代理模式 2008-01-04 14:23 yaSong
很有层次的写法,现在也很流行的写法!  回复  更多评论
  


只有注册用户登录后才能发表评论。

网站导航: