代理模式:就是为另一个对象提供一个替身或者占位符以控制对这个对象的访问。如何理解了就像买火车票,自己买不到就会找黄牛,黄牛就是代理,我们把买票这件事让黄牛代替做了。通过代码实现:
/**
* Created by apple on 2018/10/17.
*/
public interface TicktService {
public String buyTicket();
}
/**
* Created by apple on 2018/10/17.
*/
public class ProxyTicketService implements TicktService {
@Override
public String buyTicket() {
return "黄牛买票";
}
}
/**
* Created by apple on 2018/10/17.
*/
public class MineTicketService implements TicktService{
private TicktService ticktService;
public MineTicketService(TicktService ticktService) {
this.ticktService = ticktService;
}
@Override
public String buyTicket() {
return this.ticktService.buyTicket();
}
}
/**
* Created by apple on 2018/10/17.
*/
public class Main {
public static void main(String[] args){
ProxyTicketService proxyTicketService = new ProxyTicketService();
MineTicketService mineTicketService = new MineTicketService(proxyTicketService);
String result = mineTicketService.buyTicket();
System.out.println(result);
}
}
以上只是单纯的静态代理,还有另外一种动态代理,动态代理无须指定需要事前要准备好代理的类,可以在运行的时候动态的指定需要代理的类。动态代理的实现有两种方法一种是jdk提供的反射来实现,另外一种是cglib,我们分别来说一下
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* Created by apple on 2018/10/17.
*/
public class DynamicProxyService implements InvocationHandler {
private Object target;
public DynamicProxyService(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(target,args);
}
}
public class Main {
public static void main(String[] args){
ProxyTicketService proxyTicketService = new ProxyTicketService();
MineTicketService mineTicketService = new MineTicketService(proxyTicketService);
String result = mineTicketService.buyTicket();
System.out.println(result);
DynamicProxyService dynamicProxyService = new DynamicProxyService(proxyTicketService);
TicktService ticktService = (TicktService) Proxy.newProxyInstance(proxyTicketService.getClass().getClassLoader(),proxyTicketService.getClass().getInterfaces(),dynamicProxyService);
String result2 = ticktService.buyTicket();
System.out.println(result2);
}
}
反射需要继承InvocationHandler 接口需要实现其中的invoke方法,但是,在使用的过程中需要注意,通过 Proxy.newProxyInstance(classLoader,Interfaces[],InvocationHandler)
拿到需要代理的对象时第二个参数只能是接口,如果需要对类进行反射则需要用到cglib库类,cglib是一个基于asm字节码修改库的高级封装类屏蔽类底层对二进制字节码的操作细节,让开发者便于使用,Spring的AOP就是基于此类框架实现
import java.lang.reflect.Method;
/**
* Created by apple on 2018/10/17.
*/
public class CglibProxyService implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj,args);
}
}
/**
* Created by apple on 2018/10/17.
*/
public class Main {
public static void main(String[] args){
ProxyTicketService proxyTicketService = new ProxyTicketService();
MineTicketService mineTicketService = new MineTicketService(proxyTicketService);
String result = mineTicketService.buyTicket();
System.out.println(result);
DynamicProxyService dynamicProxyService = new DynamicProxyService(proxyTicketService);
TicktService ticktService = (TicktService) Proxy.newProxyInstance(proxyTicketService.getClass().getClassLoader(),proxyTicketService.getClass().getInterfaces(),dynamicProxyService);
String result2 = ticktService.buyTicket();
System.out.println(result2);
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(ProxyTicketService.class);
enhancer.setCallback(new CglibProxyService());
ProxyTicketService tkService = (ProxyTicketService) enhancer.create();
String result3 = tkService.buyTicket();
System.out.println(result3);
}
}