代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能.
 
 java中的代理模式:
 定义:给目标对象提供一个代理对象,并且由代理对象控制对目标对象的引用
 目的:①:通过代理对象的方式间接的访问目标对象,防止直接访问目标对象给系统带来不必要的复杂性
 ②:通过代理业务对原有业务进行增强
 java当中有三种方式来创建代理对象:静态代理,基于jdk(接口)的动态代理,基于CGLLIB(父类)的动态代理。
 相关概念:
 目标类:原对象,我们需要代理对象控制他的访问,拓展其功能。
 代理类:代理模式产生的对象,是原对象“替身”,已经在原有基础上修改逻辑
一、静态代理实现
 接口实现方式:
 
 以JAVA代理模拟现实生活中代购买衣服的过程:
 
 1、定义接口规定这是一个买衣服的过程
public interface ByClothes {
    void clothes(String size);
}
2、定义一个类模拟生产衣服的工厂
//目标类
public class ClothesFactory implements ByClothes {
    @Override
    public void clothes(String size) {
        System.out.println("已经为您定制一套size为"+size+"的衣服");
    }
}
3、定义一个类模拟代购员去工厂买衣服
//代理类
public class ProxyClothes implements ByClothes {
    //引进目标类对象
    public ClothesFactory factory;
    public ProxyClothes(ClothesFactory factory) {
        this.factory = factory;
    }
    @Override
    public void clothes(String size) {
        //额外功能
        FrontService();
        factory.clothes(size);
        //额外功能
        EndService();
    }
    //前置服务
    public void FrontService(){
        System.out.println("根据您的需求已做过市场调研");
    }
    //售后服务
    public void EndService(){
        System.out.println("已经为您提供售后服务");
    }
}
4、测试类Test
public class Test {
    public static void main(String[] args) {
       //创建工厂对象
        ClothesFactory clothesFactory = new ClothesFactory();
        //代购员购买衣服
        ProxyClothes proxyClothes = new ProxyClothes(clothesFactory);
        proxyClothes.clothes("XXL");
    }
}
5、输出
 
 二、实现动态代理
 承接上例,一个合格的代购公司不会仅仅有一个代购员去代购一种商品,同样的,静态代理显然有很大的缺点,他的复用性太差了,如果需要更改代理目标,那么要做出大量的修改,下面以动态代理模拟一个人去代购公司购买服装。
 
1、新建一个代购鞋子的接口
public interface ByShoot {
    void shoot(String size);
}
2、新建一个类代表鞋子工厂
public class ShootFcatory implements ByShoot {
    @Override
    public void shoot(String size) {
        System.out.println("已经为您定制一双size为"+size+"的鞋子");
    }
}
3、建立一个类代表代购公司
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//实现动态代理接口
public class ProxyFactory implements InvocationHandler {
	
    private Object factory;
	//通过set方法来选择工厂
    public void setFactory(Object factory) {
        this.factory = factory;
    }
	//重写动态代理方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    	//前置服务
        FrontService();
        //核心服务
        //通过反射来调用方法
        method.invoke(factory, args);
        //售后服务
        EndService();
        return null;
    }
    // 前置服务
    public void FrontService() {
        System.out.println("根据您的需求进行市场调研");
    }
    // 后置服务
    public void EndService() {
        System.out.println("为您提供一条龙的包办服务");
    }
    //调度员工
    public Object getProxyInstance() {
    	//调用Proxy类种的静态方法newProxyInstance()
        return Proxy.newProxyInstance(factory.getClass().getClassLoader(), factory.getClass().getInterfaces(), this);
    }
}
4、建立测试类模拟购买过程
public class Test {
    public static void main(String[] args) {
        //创建公司对象
        ProxyFactory proxyFactory = new ProxyFactory();
        //创建工厂对象
        ClothesFactory clothesFactory = new ClothesFactory();
        //选择工厂
        proxyFactory.setFactory(clothesFactory);
        //调度员工
        //通过 3 中代码可知,这个方法的返回值类型是Object,需要强转
        ByClothes emp1 = (ByClothes) proxyFactory.getProxyInstance();
        //员工购买
        emp1.clothes("XXL");
        ShootFcatory shootFcatory = new ShootFcatory();
        proxyFactory.setFactory(shootFcatory);
        ByShoot emp2 = (ByShoot) proxyFactory.getProxyInstance();
        emp2.shoot("42");
    }
5、输出
 
 以上过程便实现了动态代理的过程










