Thinking in java -- 动态代理

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

简单代理实例

package typeinfo;

//: typeinfo/SimpleProxyDemo.java
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
     */// :~

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

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

package typeinfo;

//: typeinfo/SimpleDynamicProxy.java
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
     */// :~

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值