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());