就官方描述来说:
java的常用设计模式,代理类与委托类都同样的一个接口,代理类主要为委托类 做预处理消息,消息过滤,消息转发给委托类,以及事后消息处理等。代理类与委托类存在管理关系,代理对象与委托对象有管理,代理对象并不实现服务,代理对象通过调用委托对象的相关方法实现服务。
代理模式的作用:为其他对象提供一种代理用来控制对该对象的访问,客户端不想直接或者不能直接对目标对象的方法进行访问,代理对象就是客户端与目标对象之间的桥梁。
动态代理重要的接口 InvocationHandler
该接口需实现以下方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
参数解读:
proxy:被代理的对象。
method:实现真实服务的方法
args:实现方法的参数
动态代理重要类:Proxy
Proxy类是专门完美代理的操作类,可以通过此类动态生成一个或者多个接口的实现类。提供了静态方法如下:
public static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)
throws IllegalArgumentException
参数解读:
ClassLoader loader :类加载器,用于找到被代理的类。
Class[] interfaces:得到全部接口,一个或者多个接口。
invocationHandler h:得到接口的子类(接口的实现类)
静态代理类代码说明
/**
* 接口,包含方法 sellClother()
* @author javadev
*
*/
public interface SellClotherInterface {
public void sellClother();
}
/**
* 委托类,实现类接口sellClother,服务的真正实现类,包含实现方法
* @author javadev
*
*/
public class BossFactoryService implements SellClotherInterface {
@Override
public void sellClother() {
// TODO Auto-generated method stub
System.out.println("委托类实现卖出衣服操作");
}
}
/**
* 静态代理类,实现接口SellClotherInterface,
* 使用构造方法注入委托类.
* 重写方法sellClother(),方法内通过委托类对象调用委托类方法实现服务。
*
*/
public class staticProxy implements SellClotherInterface {
private BossFactoryService bossSerive;
public staticProxy(BossFactoryService bossSerive) {
this.bossSerive = bossSerive;
}
@Override
public void sellClother() {
System.out.println("买家在代理商确认购买衣服,下订单...");
System.out.println("代理类调用委托类的方法...");
bossSerive.sellClother();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BossFactoryService bossSellClothes = new BossFactoryService();//委托类实例
SellClotherInterface scif = new staticProxy(bossSellClothes);// 代理类对象
scif.sellClother();// 代理类对象调用sellClother()方法卖衣服
}
}
动态代理类(JDK):
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 动态代理类,实现接口InvocationHandler,
* @author javadev
*
*/
public class SelClotherlProxy implements InvocationHandler {
//目标委托类
private Object target;
//生成代理类
public Object buildProxy(Object target){
this.target = target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces()
, this);//通过类加载器找到目标类,找到目标类的所有接口,找到invocationHandler接口的实现类(即代理类)。通过该类的方法newProxyInstance生成代理对象。
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("买家在代理商方确认购买衣服,下订单...");
System.out.println("通过动态代理调用委托类的方法");
Object result = method.invoke(target, args);
System.out.println("买家收到货交易结束...");
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SelClotherlProxy proxy = new SelClotherlProxy();
SellClotherInterface scif = (SellClotherInterface) proxy.buildProxy(new BossFactoryService());
scif.sellClother();
}
}
动态代理(cglib)
JDK代理是实现是依靠了接口的,但是有些类并没有接口,所以就不能使用JDK代理,cglib动态代理弥补了这一点。cgilib动态代理是对目标类生产一个子类
并覆盖其中的方法实现增强,因为是类的继承,所以不能对final修饰的类进行代理。
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* 使用cglib动态代理
*/
public class SellClotherProxy2 implements MethodInterceptor{
private Object target;
public Object buildProxy(Object target) {
this.target = target;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.target.getClass());
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("确认信息,确定下单...");
proxy.invokeSuper(obj, args);
System.out.println("买家收到货交易结束...");
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SellClotherProxy2 proxy = new SellClotherProxy2();
SellClotherInterface scif = (SellClotherInterface) proxy.buildProxy(new BossFactoryService());
scif.sellClother();
}
}