实现简单的动态代理!

这两天对java的动态代理感兴趣,自己写了个最简单的代码,认识一下动态代理!

例子:

类列表:

MyObjec是执行类。

MyProxy 是我自己实现的动态代理类,这个类实现了InvocationHandler接口,关于这个借口的描述就不多说了,可以参照api文档!好像动态代理类都实现这个接口,我是这么理解的,呵呵!

Test 类是我的业务类

ITest 是我业务类的接口!


  1. import java.lang.reflect.InvocationHandler;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.Proxy;  
  4.   
  5. public class MyObject {  
  6.   
  7.     public static void main(String[] args) {  
  8.         ITest test = new Test("kimi");  
  9.         ITest t = new MyProxy().getProxy(test);  
  10.         t.outPut();  
  11.     }  
  12. }  
  13.   
  14. class MyProxy implements InvocationHandler {  
  15.   
  16.     private ITest itest = null;  
  17.   
  18.     private Object test = null;  
  19.   
  20.     public synchronized ITest getProxy(Object o) {//用Factory的方式取代理实例,不知道做得对不对  
  21.         if (itest == null) {  
  22.             test = o;  
  23.             itest = (ITest) Proxy.newProxyInstance(  
  24.                 this.getClass().getClassLoader(),  
  25.                 o.getClass().getInterfaces(),  
  26.                 this);  
  27.             return itest;  
  28.         } else  
  29.             return itest;  
  30.     }  
  31.   
  32.     public Object invoke(Object o, Method m, Object[] aguments) throws Throwable {  
  33.   
  34.         System.out.println("my Proxy start ok!!!");  
  35.   
  36.         return m.invoke(  
  37.             test,  
  38.             aguments);  
  39.     }  
  40.   
  41. }  
  42.   
  43. class Test implements ITest {  
  44.   
  45.     private String name = null;  
  46.   
  47.     public Test(String name) {  
  48.         this.name = name;  
  49.     }  
  50.   
  51.     public void outPut() {  
  52.         System.out.println("my Test start ok!!!" + String.format("%n") + "my name is :" + this.name);  
  53.     }  
  54. }  
  55.   
  56. interface ITest {  
  57.   
  58.     public void outPut();  
  59. }  
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;  public class MyObject {      public static void main(String[] args) {         ITest test = new Test("kimi");         ITest t = new MyProxy().getProxy(test);         t.outPut();     } }  class MyProxy implements InvocationHandler {      private ITest itest = null;      private Object test = null;      public synchronized ITest getProxy(Object o) {//用Factory的方式取代理实例,不知道做得对不对         if (itest == null) {             test = o;             itest = (ITest) Proxy.newProxyInstance(                 this.getClass().getClassLoader(),                 o.getClass().getInterfaces(),                 this);             return itest;         } else             return itest;     }      public Object invoke(Object o, Method m, Object[] aguments) throws Throwable {          System.out.println("my Proxy start ok!!!");          return m.invoke(             test,             aguments);     }  }  class Test implements ITest {      private String name = null;      public Test(String name) {         this.name = name;     }      public void outPut() {         System.out.println("my Test start ok!!!" + String.format("%n") + "my name is :" + this.name);     } }  interface ITest {      public void outPut(); } 

  1. 最后,如有不妥当之处,请指示~!谢谢  
最后,如有不妥当之处,请指示~!谢谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值