代理模式之JDK的动态代理

      JDK的动态代理比较简单,是内置在JDK中的,不需要引入第三方jar包,但相对其他代理它的功能就比较弱了。

      下面就以demo为例来学习它。分为延迟加载demo和安全作用的demo

       延迟加载demo

       主题接口: 定义代理类和真实主题的公共对外方法

/**
 * 主题接口
 */
public interface IDBQuery {
    String request();
}

     真实主题:是实现真正业务逻辑的类,此次假设它是一个重量级对象

public class DBQuery implements IDBQuery {
    public DBQuery() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("真实被创建");
    }
    @Override
    public String request() {
        return "我是真实的";
    }
}

代理类:用来代理和封装真实主题,代理真实主题的位置,加快系统启动速度

/**
 * 代理角色
 */
public class DBQueryDelayProxy implements InvocationHandler {
    private IDBQuery real = null;

    public DBQueryDelayProxy() {
        System.out.println("代理被创建");
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(real == null) {
            real = new DBQuery();
        }
        return real.request();
    }
}

 客户端:

 @Test
    public void jdkDelayProxyTest() {
        IDBQuery jdkProxy = (IDBQuery) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),new Class[] {IDBQuery.class},
                new DBQueryDelayProxy());
        jdkProxy.request();
    }

      中间层作用demo(安全)

       主题接口:是定义代理类和真实主题的公共对外方法

public interface JDKInterface {
    /**
     * 出售房
     */
    void sell();

    /**
     * 和租客碰面
     */
    void contact();

    /**
     * 收费
     */
    void charge();
}

真实主题:是真正实现业务逻辑的类

public class JDKHouseOwner implements JDKInterface {
    @Override
    public void sell() {
        System.out.println("售卖的相关事");
    }

    @Override
    public void contact() {
        System.out.println("碰面");
    }

    @Override
    public void charge() {
        System.out.println("房屋出租价格范围");
    }
}

 代理类: 用来代理和封装真实主题,可以起到保护真实主题的作用,也可以做延迟加载。本次以安全作用为例:

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

public class JDKIntermediary implements InvocationHandler {
    private JDKInterface real;
    public JDKIntermediary(JDKInterface real) {
        this.real = real;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        method.invoke(real,args);
        return null;
    }
}

 客户端:

    @Test
    public void jdkProxyTest() {
        JDKInterface real = new JDKHouseOwner();
        InvocationHandler invocationHandler = new JDKIntermediary(real);
        JDKInterface jdkInterface = (JDKInterface) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),new Class[] {JDKInterface.class},
                invocationHandler);
        jdkInterface.sell();
        jdkInterface.contact();
        jdkInterface.charge();
    }

代理类的内部逻辑是用JDKIntermediary决定的。生成代理后,由 Proxy.newProxyInstance()返回一个代理类的实例。

public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)
loader: the class loader to define the proxy class
interfaces: the list of interfaces for the proxy class to implement
h: the invocation handler to dispatch method invocations to

猜你喜欢

转载自blog.csdn.net/xshsjl/article/details/85062007