java 动态代理

1、代理模式

interface BuyerInterface {
    String buySomething(String goods);
}

class Buyer implements BuyerInterface {
    @Override
    public String buySomething(String goods) {
        System.out.println("buy a " + goods);
        return goods;
    }
}

class BuyProxy implements BuyerInterface {
    private Buyer trueBuyer;
    public BuyProxy(Buyer trueBuyer) {
        this.trueBuyer = trueBuyer;
    }

    @Override
    public String buySomething(String goods) {
        System.out.println("find true  business");
        this.trueBuyer.buySomething(goods);
        System.out.println("buy a " + goods + " success");
        return goods;
    }
}

public class PrxyDemo {
    public static void main(String[] args) throws Exception {
        BuyerInterface buyProxy = new BuyProxy(new Buyer());
        buyProxy.buySomething("TV");
        System.out.println("**********");
}

2、java 动态代理
2.1 InvocationHandler 实现类告诉程序运行动态生成的代理对象具体要执行什么动作。
“Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the {@code invoke} method of its invocation handler”

class BuyInvocationHandler implements InvocationHandler {

    private Object target;

    public BuyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("find true  business");
        String result = (String) method.invoke(target, args);
        System.out.println("buy a " + args[0] + " success");
        System.out.println(result.toString());
        return result;
    }
}

2.2 动态代理对象生成
InvocationHandler handler = new BuyInvocationHandler(target);
step1:生成字节码文件
Class proxyClazz = Proxy.getProxyClass(BuyerInterface.class.getClassLoader(), BuyerInterface.class);
step2: 获取相应的构造函数类 Constructor的实例
Constructor constructor = proxyClazz.getConstructor(InvocationHandler.class);
BuyerInterface buyProxy1 = (BuyerInterface)
step:3 通过构造函数实例生成BuyerInterface的实现类实例,即代理对象实例。
constructor.newInstance(handler);
上面的step 1、2、 3 合并:
BuyerInterface buyProxy2 = (BuyerInterface) Proxy
.newProxyInstance(BuyerInterface.class.getClassLoader(), new Class[]{BuyerInterface.class}, handler);


public class PrxyDemo {

    public static void main(String[] args) throws Exception {
        Buyer target = new Buyer();
        InvocationHandler handler = new BuyInvocationHandler(target);
        Class proxyClazz = Proxy.getProxyClass(BuyerInterface.class.getClassLoader(), BuyerInterface.class);
        Constructor constructor = proxyClazz.getConstructor(InvocationHandler.class);
        BuyerInterface buyProxy1 = (BuyerInterface) constructor.newInstance(handler);
        buyProxy1.buySomething("TV");
        System.out.println("/////////");
        BuyerInterface buyProxy2 = (BuyerInterface) Proxy
            .newProxyInstance(BuyerInterface.class.getClassLoader(), new Class[]{BuyerInterface.class}, handler);
        buyProxy2.buySomething("TV");
    }
}




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版权声明:本文为博主原创文章,未经博主允许不得转载 前言 Java 代理模式在 Android 中有很多的应用。比...
    cc荣宣阅读 879评论 0 7
  • 设计模式文章陆续更新 java单例模式java工厂模式java状态模式 这几天在看一些框架源码时看到了一个很奇妙的...
    林锐波阅读 1,021评论 0 10
  • 事例 小张是一个普普通通的码农,每天勤勤恳恳地码代码。某天中午小张刚要去吃饭,一个电话打到了他的手机上。“是XX公...
    余平的余_余平的平阅读 505评论 0 0
  • 什么是代理模式? 其实代理和我们的生活息息相关,简单来说就是:中介,比如我们要租房子,我们找中介公司就可以了,比如...
    星星_点灯阅读 282评论 0 1
  • 我丫就吃了一顿好的,这痘就发疯了的长,旧的还没去,新的又来了,我青春期痘都没长 。 这段时间,因为刚辞职,然后大家...
    1大太阳阅读 267评论 0 1