java动态代理

动态代理

public class MyInvocationHandler implements InvocationHandler {
 private Object object=null;
 public Object bind(Object obj){
  object=obj;
  return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
 }
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Object temp=method.invoke(this.object, args);
  return temp;
 }

}

通过bind方法绑定代理类

 MyInvocationHandler handler=new MyInvocationHandler();
        Subject subject=(Subject)handler.bind(new RealSubject());

 

posted @ 2017-05-14 22:28  cindy_zhu  阅读(111)  评论(0编辑  收藏  举报