Proxy模式(动态代理)
这是JDK自带的Proxy模式的实现方法
例子
view plaincopy to clipboardprint?
public interface GirlInfo {
public void hasBoyFriend();
}
public class Girl implements GirlInfo {
public void hasBoyFriend() {
Systemoutprintln(还没有男朋友);
}
}
public class GirlProxy implements InvocationHandler {
private Object delegate;
public Object bind(Object delegate) {
thisdelegate = delegate;
return ProxynewProxyInstance(delegategetClass()getClassLoader()
delegategetClass()getInterfaces() this);
}
public Object invoke(Object proxy Method method Object[] args)
throws Throwable {
methodinvoke(delegate args);
return null;
}
}
public class ProxyClient {
public static void main(String[] args) {
GirlProxy girlProxy = new GirlProxy();
GirlInfo girl = (GirlInfo) girlProxybind(new Girl());
girlhasBoyFriend();
}
}