code from 《Thinking in java》

代理模式

interface Interface {
void doSomething();
void somethingElse(String arg);
} class RealObject implements Interface { @Override
public void doSomething() {
System.out.println("doSomething.");
} @Override
public void somethingElse(String arg) {
System.out.println("somethingElse " + arg);
}
} class SimpleProxy implements Interface { private Interface proxy; public SimpleProxy(Interface proxy) {
this.proxy = proxy;
} @Override
public void doSomething() {
System.out.println("SimpleProxy doSomething.");
proxy.doSomething();
} @Override
public void somethingElse(String arg) {
System.out.println("SimpleProxy somethingElse " + arg);
proxy.somethingElse(arg);
}
} public class SimpleProxyDemo { public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
} public static void main(String[] args) {
consumer(new RealObject());
consumer(new SimpleProxy(new RealObject()));
} }

输出:

doSomething.
somethingElse bonobo
SimpleProxy doSomething.
doSomething.
SimpleProxy somethingElse bonobo
somethingElse bonobo

动态代理

class DynamicProxyHandler implements InvocationHandler {

    private Object proxy;

    public DynamicProxyHandler(Object proxy) {
this.proxy = proxy;
} @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("*** proxy: " + proxy.getClass() +
". method: " + method + ". args: " + args);
if(args != null) {
for(Object arg : args)
System.out.println(" " + arg);
}
return method.invoke(this.proxy, args);
}
} public class SimpleDynamicProxy { public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
} public static void main(String[] args) {
RealObject real = new RealObject();
consumer(real);
// insert a proxy and call again:
Interface proxy = (Interface)Proxy.newProxyInstance(
Interface.class.getClassLoader(),
new Class[]{ Interface.class },
new DynamicProxyHandler(real)); consumer(proxy);
} }

输出:

doSomething.
somethingElse bonobo
*** proxy: class typeinfo.\$Proxy0. method: public abstract void typeinfo.Interface.doSomething(). args: null
doSomething.
*** proxy: class typeinfo.\$Proxy0. method: public abstract void typeinfo.Interface.somethingElse(java.lang.String). args: [Ljava.lang.Object;@6a8814e9
bonobo
somethingElse bonobo

二者最大的区别 动态代理 将代理类和实际执行类 解耦了  

代理类持有的是Object类型的引用 也就是说此时该代理类可以代理不同类型的实际执行类  

只需要在客户端实例化不同的实际执行类 再传入代理类中即可 

key thinking :解耦

最新文章

  1. 使用Spark分析拉勾网招聘信息(三): BMR 入门
  2. HTML <b>、 <strong> 、<big>、<small>、<em>、<i>、<sub>和<sup> 标签
  3. 检测是否是IE浏览器
  4. OpenCV成长之路(9):特征点检测与图像匹配
  5. node 事件循环
  6. 利用shell脚本统计文件中出现次数最多的IP
  7. 【PHPsocket编程专题(实战篇②)】兼容 Curl/Socket/Stream 的 HTTP 操作类[转]
  8. Eclipse代码自动提示
  9. Linux定时任务编写
  10. Linux下使用JNI的常见问题及解决方案
  11. java基础(五章)
  12. Sass学习笔记(补充)
  13. [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions
  14. Oh-My-Zsh及主题、插件安装与配置
  15. D6差分及树上差分
  16. [CQOI2014]排序机械臂
  17. websphere部署war包
  18. POJ1430
  19. CF961E Tufurama【主席树】
  20. 第十一章 AtomicInteger源码解析

热门文章

  1. SpringMVC表单中post请求转换为put或delete请求
  2. Android笔记: ListView基本用法-ArrayAdapter
  3. Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
  4. Centos7.2下Nginx配置SSL支持https访问(站点是基于.Net Core2.0开发的WebApi)
  5. 解决tomcat的安装文件中点击startup.bat闪退的问题
  6. js实现非模态窗口增加数据后刷新父窗口数据
  7. HDU 6185 Covering 矩阵快速幂
  8. Jmeter遇到的坑
  9. JS中 事件冒泡与事件捕获
  10. java中的jdk切换(无需卸载原有jdk)