Java 代理模式

2021-02-08  本文已影响0人  索性流年

文集地址

一句话总计代理模式

什么是代理模式?

*为其他对象生成一个代理,以控制这个对象的访问

为什么使用代理模式?

代理模式实现原理

生么是静态代理?

静态代理的缺点?

动态代理

代理模式两种创建方式

静态代理与动态代理区别

JDK动态代理与CGLIB代理区别

JDK动态代理实现原理

CGLIB代理实现原理

应用案例

实现案例

/**
 * 被代理对象
 *
 * @author ext.liuyan10
 * @date 2021/2/5 14:34
 */
public class User {
    public void test() {
        System.out.println("小朋友:糖真甜");
    }
}

静态代理

/**
 * 代理对象
 *
 * @author ext.liuyan10
 * @date 2021/2/5 13:38
 */
public class UserProxy implements UserApi {
    private User user;

    public UserProxy(User user) {
        this.user = user;
    }

    @Override
    public void test() {
        System.out.println("将糖找来");
        System.out.println("撕开糖纸,喂给小朋友");
        user.test();
    }
}

/**
 * @author liunian
 * @date 2021/2/5 13:42
 */
public class TestApp {

    public static void main(String[] args) {
        User user = new User();
        UserProxy userProxy = new UserProxy(user);
        userProxy.test();
    }
}

动态代理

CGLIB动态代理

  <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.10</version>
  </dependency>
/**
 * 生成动态代理对象
 *
 * @author liunian
 * @date 2021/2/5 14:35
 */
public class UserCglibInvocation implements MethodInterceptor {
    private Object target;

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

    public Object getProxyInstance() {
        //工具类
        Enhancer en = new Enhancer();
        //设置父类
        en.setSuperclass(target.getClass());
        //设置回调函数
        en.setCallback(this);
        //创建子类代理对象
        return en.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("将糖找来");
        System.out.println("撕开糖纸,喂给小朋友");
        Object invoke = method.invoke(target);
        return invoke;
    }
}

/**
 * @author liunian
 * @date 2021/2/5 13:42
 */
public class TestApp {

    public static void main(String[] args) {
        User user = new User();
        UserCglibInvocation userInvocation = new UserCglibInvocation(user);
        User proxyInstance = (User) userInvocation.getProxyInstance();
        proxyInstance.test();
    }
}

JDK动态代理
/**
 * 被代理对象 
 *
 * @author liunian
 * @date 2021/2/5 13:39
 */
public interface UserApi {
    void test();
}
/**
 * 被代理对象实现
 *
 * @author liunian
 * @date 2021/2/5 13:36
 */
public class UserImpl implements UserApi{
    @Override
    public void test() {
        System.out.println("小朋友:糖真甜");
    }
}
/**
 * 生成代理对象
 *
 * @author ext.liuyan10
 * @date 2021/2/5 14:03
 */
public class UserInvocation implements InvocationHandler {

    Object target;

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("将糖找来");
        System.out.println("撕开糖纸,喂给小朋友");
        Object invoke = method.invoke(target);
        return invoke;
    }
}

/**
 * @author liunian
 * @date 2021/2/5 13:42
 */
public class TestApp {
    public static void main(String[] args) {
        UserApi user = new UserImpl();
        UserInvocation userInvocation = new UserInvocation(user);
        UserApi userApi = (UserApi) Proxy.newProxyInstance(user.getClass().getClassLoader(), user.getClass().getInterfaces(), userInvocation);
        userApi.test();
    }
}
Connected to the target VM, address: '127.0.0.1:51870', transport: 'socket'
将糖找来
撕开糖纸,喂给小朋友
小朋友:糖真甜
Disconnected from the target VM, address: '127.0.0.1:51870', transport: 'socket'

Process finished with exit code 0

上一篇下一篇

猜你喜欢

热点阅读