think in java笔记:Dynamic Proxy

think in java笔记:Dynamic Proxy

proxy是什么?

Proxy is one of the basic design patterns. It is an object that you insert in place of the “real”
object in order to provide additional or different operations—these usually involve
communication with a “real” object, so a proxy ty pically acts as a go-between.

译:
Proxy是一个基本的设计模式. Proxy是一个持有真正对象的对象,扮演中间人的角色,实现与真正对象的互动。

A proxy can be helpful anytime you’d like to separate extra operations into a different place
than the “real object,” and especially when you want to easily change from not using the extra
operations to using them, and vice versa (the point of design patterns is to encapsulate
change—so you need to be changing things in order to justify the pattern). For example, what
if you wanted to track calls to the methods in the RealObject, or to measure the overhead of
such calls? This is not code you want to have incorporated in your application, so a proxy
allows you to add and remove it easily.

译:
一个proxy可以是很有用的,尤其是你希望增加一个额外的操作但是不放到真正的对象里,或者当你希望很容易的没有额外操作到有额外操作,或者通过(proxy的设计模式是为了包装变化,所以你可能需要变动东西来符合这种模式).举例来说,如果你希望追踪真正对象的调用次数,或者来测试这些调用的过用程度.这些代码都不是你所想写在应用中的,代理可以让你很容易的添加或者删除。
(总的来说,就是我们把一个真正对象插入到代理中,然后通过代理的方式进行调用。这样子可以很容易增加一些额外与应用无关的功能)

Java’s dynamic proxy takes the idea of a proxy one step further, by both creating the proxy
object dynamically and handling calls to the proxied methods dynamically. All calls made on a
dynamic proxy are redirected to a single invocation handler , which has the job of discovering
what the call is and deciding what to do about it.

译:
Java的动态代理是在代理的基础上更进一步,可以自动创建proxy并且处理动态代理方法。所有调用动态代理的都会被转发给一个invocation handler, 用来寻找这个调用什么,需要做什么.



附录:

简单代理代码:

import static net.mindview.util.Print.*; 

interface Interface { 
  void doSomething(); 
  void somethingElse(String arg); 
 
} 

class RealObject implements Interface { 
  public void doSomething() { print("doSomething"); } 
  public void somethingElse(String arg) { 
    print("somethingElse " + arg); 
  } 
}  

class SimpleProxy implements Interface { 
  private Interface proxied; 
  public SimpleProxy(Interface proxied) { 
    this.proxied = proxied; 
  } 
  public void doSomething() { 
    print("SimpleProxy doSomething"); 
    proxied.doSomething(); 
  } 
  public void somethingElse(String arg) { 
    print("SimpleProxy somethingElse " + arg); 
    proxied.somethingElse(arg); 
  } 
}  

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())); 
  } 
} /* Output: 
doSomething 
somethingElse bonobo 
SimpleProxy doSomething 
doSomething 
SimpleProxy somethingElse bonobo 
somethingElse bonobo 
*///:~

一个proxy继承对象的interface,然后就可以cast为interface,并且还把对象插入到其中,所以可以被像interface一样使用

简单动态代理代码:

import java.lang.reflect.*; 

class DynamicProxyHandler implements InvocationHandler { 
  private Object proxied; 
  public DynamicProxyHandler(Object proxied) { 
    this.proxied = proxied; 
  } 
  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(proxied, args); 
  } 
}  

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); 
  } 
} /* Output: (95% match)   
doSomething 
somethingElse bonobo 
**** proxy: class $Proxy0, method: public abstract void 
Interface.doSomething(), args: null 
doSomething 
**** proxy: class $Proxy0, method: public abstract void 
Interface.somethingElse(java.lang.String), args: 
[Ljava.lang.Object;@42e816 
  bonobo 
somethingElse bonobo 
*///:~

通过使用Proxy的静态方法构造一个proxy,这个proxy有三个参数:1.classLoader 2.proxy需要实现的interface 3.动态代理的handler。这样子就会自动构造出一个类似于简单代理的proxy,可以像interface一样使用。具体的代码在handler中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值