Java代理和动态代理
2016-08-15 15:31
363 查看
code from 《Thinking in java》
代理模式
输出:
doSomething.
somethingElse bonobo
SimpleProxy doSomething.
doSomething.
SimpleProxy somethingElse bonobo
somethingElse bonobo
动态代理
输出:
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 :解耦
代理模式
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 :解耦
相关文章推荐
- Java中信号量Semaphore的简单使用
- java实现:将一个数逆序输出
- java实现:将一个数逆序输出
- java实现:将一个数逆序输出
- java答疑
- java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 出现错误
- Retrofit结合RxJava个人使用经验
- 第一个Java程序
- 第一个Java程序
- 第一个Java程序
- java基础——实现线程串行执行
- Java 面向对象
- Spring+SpringMVC
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- java基础——串行与并行
- Eclipse编程助手
- Java 面向对象之 继承
- 可见性修饰符
- JavaEE——properties配置文件的编写和解析获取
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)