代理模式(Proxy Pattern)

代理模式:为其他对象提供一种代理以控制对这个对象的访问。(来自百度百科)

   通过代理对象访问目标对象,为目标对象添加额外的功能。

例如买一套房子,自己找房子花费100万。中介为客户提供房源,额外收取1%的佣金。这种通过中介购买的行为就是代理模式。

1,买房子接口

public interface BuyHouse {
    void buyHouse();
}

2, 买房子实现类

public class BuyHouseImpl implements BuyHouse {
    @Override
    public void buyHouse() {
        System.out.println("花费100万");
    }
}

3, 代理模式实现一共有三种,分别是静态代理,动态代理,CGLIB代理。

 3.1 静态代理

  1)代理类:代理对象中存在被代理对象的实例(也可以作为参数传入)

public class BuyHouseProxy  implements BuyHouse {

    private BuyHouse buyHouse;

    public BuyHouseProxy() {
        this.buyHouse = new BuyHouseImpl();
    }

    @Override
    public void buyHouse() {
        buyHouse.buyHouse() ;
        System.out.println("中介再收取1%的佣金");
    }
}

  2)测试静态代理

public class StaticProxy {
    public static void main(String[] args) {
        //BuyHouse buyHouse = new BuyHouseImpl(); //直接买房
        //buyHouse.buyHouse();
        BuyHouse proxy = new BuyHouseProxy();   //找中介买房
        proxy.buyHouse();
    }
}

  3)测试结果

花费100万买了房
中介再收取1%的佣金

Process finished with exit code 0

3.2 动态代理(需要优化)

  动态代理利用JDK的API,动态的在内存中构建代理对象。

  代理对象并不需要实现接口,但目标对象一定要实现接口,否则不能使用动态代理。

  代理对象使用newProxyInstance方法要指定三个参数,分别是目标对象使用类加载器,目标对象实现的接口的类型,使用泛型方式确认类型,事件处理。

public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)

  1)创建动态代理对象实现InvocationHandler接口

public class BuyHouseDynamicProxy implements InvocationHandler {

    private Object object;
    public BuyHouseDynamicProxy(final Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(object, args);
        System.out.println("中介再收取1%的佣金");
        return result;
    }
}

  2)测试动态代理

public class DynamicProxy {
    public static void main(String[] args) {
        BuyHouse buyHouse = new BuyHouseImpl();
        BuyHouse proxyInstance = (BuyHouse)Proxy.newProxyInstance(BuyHouse.class.getClassLoader()
        , new Class[]{BuyHouse.class}, new BuyHouseDynamicProxy(buyHouse)); proxyInstance.buyHouse(); } }

  3)测试结果

花费100万买了房
中介再收取1%的佣金

Process finished with exit code 0

3.3 Cglib代理

  CGLIB创建的动态代理对象比JDK创建的动态代理对象的性能更高,但是CGLIB创建代理对象时所花费的时间却比JDK多得多。

  除了考虑目标对象是否实现接口以外,对于单例的对象,因为无需频繁创建对象,用CGLIB合适,反之使用JDK方式要更为合适一些。

  同时由于CGLib由于是采用动态创建子类的方法,对于final和static修饰的方法无法进行代理。

  

  1)创建CglibInterceptor拦截器

public class CglibInterceptor implements MethodInterceptor {
    private Object target;

    public Object getInstance(final Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer(); //enhancer [ɪnˈhænsə(r)] 增加者
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result = methodProxy.invoke(target, objects);
        System.out.println("中介再收取1%的佣金");
        return result;
    }
}

  2)测试Cglib

public class CglibProxy {

    public static void main(String[] args) {
        BuyHouse buyHouse = new BuyHouseImpl();
        CglibInterceptor cglibInterceptor = new CglibInterceptor();
        BuyHouseImpl buyHouseCglibProxy = (BuyHouseImpl) cglibInterceptor.getInstance(buyHouse);
        buyHouseCglibProxy.buyHouse();
    }

}

  3)测试结果

花费100万买了房
中介再收取1%的佣金

Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/handler4J/p/10302507.html