代理模式

代理模式
代理类,可以代理任何接口封装的类
 1 package  com.lucky.proxy.util;
 2
 3 import  java.lang.reflect.InvocationHandler;
 4 import  java.lang.reflect.Method;
 5 import  java.lang.reflect.Proxy;
 6
 7 public   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
接口
1 package  com.lucky.proxy;
2
3 public   interface  IProxyClass  {
4    public String sayHello(String str);
5    public void print();
6    public String getInnerStr();
7}

8
接口实现类
 1 package  com.lucky.proxy;
 2
 3 public   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
测试类
 1 package  com.lucky.proxy.test;
 2
 3 import  com.lucky.proxy.IProxyClass;
 4 import  com.lucky.proxy.IProxyClass2;
 5 import  com.lucky.proxy.ProxyClass;
 6 import  com.lucky.proxy.ProxyClass2;
 7 import  com.lucky.proxy.util.ProxyMode;
 8
 9 public   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
用反射实现代理模式
 1 package  com.lucky.proxy.util;
 2
 3 public   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

你可能感兴趣的:(代理模式)