代理模式

1.定义

代理模式是指为某个对象提供一个代理对象,来起到间接访问的一个目的,属于结构型设计模式,在某种情况下,一个对象不适合或者不能直接引用目标对象,代理对象在客户端和目标对象中间起到了一个中介的作用

2.静态代理

静态代理就是硬编码,在编码的过程中就已经知道要代理的对象,最常见的就是controller、service、dao 就是静态代理的一种实现。

/**
 * create by yufeng on 2021/7/4 14:00
 */
public interface IPerson {

    void findLove();

}
/**
 * create by yufeng on 2021/7/4 14:00
 */
public class ZhangSan implements IPerson {

    public void findLove() {
        System.out.println("儿子要求:肤白貌美大长腿");
    }

}
/**
 * create by yufeng on 2021/7/4 14:00
 */
public class ZhangLaosan implements IPerson {

    private ZhangSan zhangsan;

    public ZhangLaosan(ZhangSan zhangsan) {
        this.zhangsan = zhangsan;
    }

    public void findLove() {
        System.out.println("张老三开始物色");
        zhangsan.findLove();
        System.out.println("开始交往");
    }

}
/**
 * create by yufeng on 2021/7/4 14:00
 */
public class Test {
    public static void main(String[] args) {
        ZhangLaosan zhangLaosan = new ZhangLaosan(new ZhangSan());
        zhangLaosan.findLove();
    }
}
执行结果:
  张老三开始物色
    儿子要求:肤白貌美大长腿
    开始交往
3.动态代理
3.1jdk动态代理

静态代理的弊端,就是静态代理只能手动增加,如果代理类增加了新的方法,代理类也要同步增加,违背了开闭原则

动态代理在代码允许的过程中动态生成代码的形式,取消了对被代理类的扩展限制,遵循开闭原则。

/**
 * create by yufeng on 2021/7/4 14:33
 */
public interface IPerson {

    void findLove();

    void buyInsure();

}
/**
 * create by yufeng on 2021/7/4 14:33
 */
public class Zhangsan implements IPerson {

    public void findLove() {
        System.out.println("张三要求:肤白貌美大长腿");
    }

    public void buyInsure() {
        System.out.println("30万");
    }

}
/**
 * create by yufeng on 2021/7/4 14:33
 */
public class ZhaoLiu implements IPerson {

    public void findLove() {
        System.out.println("赵六要求:有车有房学历高");
    }

    public void buyInsure() {

    }

}
/**
 * create by yufeng on 2021/7/4 14:33
 */
public class JdkMeipo implements InvocationHandler {
    private IPerson target;
    public IPerson getInstance(IPerson target){
        this.target = target;
        Class clazz =  target.getClass();
        return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(this.target,args);
        after();
        return result;
    }

    private void after() {
        System.out.println("双方同意,开始交往");
    }

    private void before() {
        System.out.println("我是媒婆,已经收集到你的需求,开始物色");
    }
}

/**
 * create by yufeng on 2021/7/4 14:33
 */
public class Test {
    public static void main(String[] args) {
        JdkMeipo jdkMeipo = new JdkMeipo();
        IPerson zhangsan = jdkMeipo.getInstance(new Zhangsan());
        zhangsan.findLove();
        zhangsan.buyInsure();


        IPerson zhaoliu = jdkMeipo.getInstance(new ZhaoLiu());
        zhaoliu.findLove();

    }
}
执行结果:
  我是媒婆,已经收集到你的需求,开始物色
    张三要求:肤白貌美大长腿
    双方同意,开始交往
    我是媒婆,已经收集到你的需求,开始物色
    30万
    双方同意,开始交往
    我是媒婆,已经收集到你的需求,开始物色
    赵六要求:有车有房学历高
    双方同意,开始交往

动态生成代理类,后面如有王五要相亲,是不需要改动代理类的。

3.2cglib 动态代理
/**
 * create by yufeng on 2021/7/4 14:33
 */
public class Customer {

    public void findLove(){
        System.out.println("儿子要求:肤白貌美大长腿");
    }
}
/**
 * create by yufeng on 2021/7/4 15:33
 */
public class CGlibMeipo implements MethodInterceptor {


    public Object getInstance(Class clazz) throws Exception{
        //相当于Proxy,代理的工具类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object obj = methodProxy.invokeSuper(o,objects);
        after();
        return obj;
    }

    private void before(){
        System.out.println("我是媒婆,我要给你找对象,现在已经确认你的需求");
        System.out.println("开始物色");
    }

    private void after(){
        System.out.println("OK的话,准备办事");
    }
}

/**
 * create by yufeng on 2021/7/4 14:33
 */
public class CglibTest {
    public static void main(String[] args) {

        try {
         
            Customer obj = (Customer) new CGlibMeipo().getInstance(Customer.class);
            System.out.println(obj);
            obj.findLove();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
3.3两种动态代理的区别

JDK是采用读取接口的信息;CGLib覆盖父类方法。目的都是动态生成一个新的类,去实现增加代码逻辑的功能。

JDK对应目标类来说必须实现某个接口;CGLib可以代理任意一个普通的类,没有特殊要求。

CGLib生成的代理逻辑更复杂,效率调用效率更高,生成一个抱哈所有逻辑的FastClass,不需要反射

JDK Proxy生成代理的逻辑简单,执行效率相对要低,每次都要反射动态调用

但是CGLib 有需要注意的地方,CGLib不能代理final的方法

4源码地址

代理模式源码地址

你可能感兴趣的:(代理模式)