java 中动态代理

2017-11-20  本文已影响0人  zhouzhuo933

1 普通实现方式


public interface WomanKind {
    void guangjie();
    void gouwu();
}

2 两个实现类

public class LiNvShi implements WomanKind {
    @Override
    public void guangjie() {
        System.out.println("李女士去逛街");
    }

    @Override
    public void gouwu() {
        System.out.println("李女士去购物");
    }
}
public class WangNvShi implements WomanKind {
    @Override
    public void guangjie() {
        System.out.println("王女士去逛街");
    }

    @Override
    public void gouwu() {
        System.out.println("王女士去购物");
    }
}

3 两个代理类

public class ProxyOne implements WomanKind {
    private WomanKind womanKind;

    public ProxyOne(WomanKind womanKind) {
        this.womanKind = womanKind;
    }

    public void guangjie() {
        System.out.println("代理1 逛街预处理");
        this.womanKind.guangjie();
        System.out.println("代理1 逛街善后");
    }

    public void gouwu() {
        System.out.println("代理1 购物预处理");
        this.womanKind.gouwu();
        System.out.println("代理1 购物善后");
    }
}

public class ProxyTwo implements WomanKind {
    private WomanKind womanKind;

    public ProxyTwo(WomanKind womanKind) {
        this.womanKind = womanKind;
    }

    public void guangjie() {
        System.out.println("代理2 逛街预处理");
        this.womanKind.guangjie();
        System.out.println("代理2 逛街善后");
    }

    public void gouwu() {
        System.out.println("代理2 购物预处理");
        this.womanKind.gouwu();
        System.out.println("代理2 购物善后");
    }
}

4 测试

       WangNvShi wangNvShi = new WangNvShi();
      LiNvShi liNvShi = new LiNvShi();
      System.out.println("==========实现1=============");
        ProxyOne proxyOne = new ProxyOne(liNvShi);
        proxyOne.gouwu();
        proxyOne.guangjie();
        ProxyTwo proxyTwo = new ProxyTwo(wangNvShi);
        proxyTwo.gouwu();
        proxyTwo.guangjie();

11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: ==========实现1=============
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 购物预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 李女士去购物
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 购物善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 李女士去逛街
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 购物预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 王女士去购物
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 购物善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 王女士去逛街
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街善后

5 动态代理方式实现


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class DynamicProxy implements InvocationHandler {
    private Object object;
    private String proxyName;

    public DynamicProxy(Object object, String proxyName) {
        this.object = object;
        this.proxyName = proxyName;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(this.proxyName + " 逛街预处理");
        method.invoke(this.object, args);
        System.out.println(this.proxyName + " 逛街善后");
        return null;
    }
}

6 测试


        System.out.println("=============实现2==========");
        Class c = wangNvShi.getClass();
        DynamicProxy dynamicProxy = new DynamicProxy(liNvShi, "代理1");
        WomanKind womanKind = (WomanKind) Proxy.newProxyInstance(c.getClassLoader(), c.getInterfaces(), dynamicProxy);
        womanKind.gouwu();
        womanKind.guangjie();
        dynamicProxy = new DynamicProxy(wangNvShi, "代理2");
        womanKind = (WomanKind)Proxy.newProxyInstance(c.getClassLoader(), c.getInterfaces(), dynamicProxy);
        womanKind.gouwu();
        womanKind.guangjie();
    }
}
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: =============实现2==========
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 李女士去购物
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 李女士去逛街
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理1 逛街善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 王女士去购物
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街善后
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街预处理
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 王女士去逛街
11-20 10:46:45.730 7874-7874/com.zhouzhuo.javaaop_master I/System.out: 代理2 逛街善后
上一篇 下一篇

猜你喜欢

热点阅读