java

位置:IT落伍者 >> java >> 浏览文章

Java设计模式-----Proxy模式(动态代理)


发布日期:2020年07月22日
 
Java设计模式-----Proxy模式(动态代理)

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

}

}

               

上一篇:使用实时Java降低Java应用程序的易变性(2)

下一篇:Java中怎么遍历map中value值