Thinking in java -- 动态代理

来源:互联网 发布:javiewer差不多的软件 编辑:程序博客网 时间:2024/05/29 15:36

代理,不直接去调用某个类的方法,而由另外一个类代办理的操作,我们可以理解为代理。

简单代理实例

package typeinfo;//: typeinfo/SimpleProxyDemo.javaimport 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     */// :~

SimplePrxoy类担任了代理类的职责,替我们完成了调用。

java api为我们提供了动态代理的方法,示例如下

package typeinfo;//: typeinfo/SimpleDynamicProxy.javaimport 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     */// :~

1、实现了InvocationHandler接口。
2、Proxy.newProxyInstance获取动态代理类,类型是Object,我们强转成,我们要的类,然后进行后续操作。

代码来自”《Thinking in java》14.7-动态代理”章节

0 0
原创粉丝点击