1.代理模式的基本介绍
2.静态代理
静态代理在使用时,需要定义接口或者父类,被代理对象(即目标对象)与代理对象一起实现相同的接口或者是继承相同父类
具体要求
package proxy;
public interface ITeacherDao {
void teach();
}
package proxy;
public class TeacherDao implements ITeacherDao{
@Override
public void teach() {
System.out.println("老师授课中...");
}
}
package proxy;
public class TeacherDaoProxy implements ITeacherDao{
private ITeacherDao target;//目标对象,通过接口来聚合
public TeacherDaoProxy(ITeacherDao target) {
this.target = target;
}
@Override
public void teach() {
System.out.println("开始代理 完成某些操作...");
target.teach();
System.out.println("提交...");
}
}
package proxy;
public class Client {
public static void main(String[] args) {
//创建目标对象(被代理对象)
TeacherDao teacherDao = new TeacherDao();
//创建代理对象,同时将被代理对象传给代理对象
TeacherDaoProxy teacherDaoProxy = new TeacherDaoProxy(teacherDao);
//通过代理对象,调用到被代理对象的方法
//即:执行的是代理对象的方法,代理对象再去调用目标对象的方法
teacherDaoProxy.teach();
}
}
静态代理优缺点
3.动态代理
动态代理模式的基本介绍
JDK 中生成代理对象的API
package proxy.dynamic;
public interface ITeacherDao{
void teach();
}
package proxy.dynamic;
public class TeacherDao implements ITeacherDao{
@Override
public void teach() {
System.out.println("老师授课中...");
}
}
package proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory {
//维护一个目标对象,Object
private Object target;
//构造器,对target进行初始化
public ProxyFactory(Object target) {
this.target = target;
}
//给目标对象生成一个代理对象
public Object getProxyInstance(){
/* public static Object newProxyInstance(ClassLoader loader,
Class>[] interfaces,
InvocationHandler h)
1.ClassLoader loader:指定当前目标对象使用的类加载器
2.Class>[] interfaces:目标对象实现的接口类型
3.InvocationHandler h:处理目标对象的方法
*/
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("jdk代理开始");
//反射机制调用目标对象的方法
Object returnVal = method.invoke(target, args);
return returnVal;
}
});
}
}
package proxy.dynamic;
public class Client {
public static void main(String[] args) {
//创建目标对象
ITeacherDao target = new TeacherDao();
//给目标对象创建代理对象
ITeacherDao proxyInstance = (ITeacherDao) new ProxyFactory(target).getProxyInstance();
//通过代理对象,调用目标对象的方法
proxyInstance.teach();
}
}
4.Cglib代理
Cglib代理模式的基本介绍
Cglib代理模式实现步骤
package proxy.cglib;
public class TeacherDao {
public void teach(){
System.out.println("老师授课中,我是cglib代理,不需要实现接口");
}
}
package proxy.cglib;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class ProxyFactory implements MethodInterceptor {
//维护一个目标对象
private Object target;
//构造器,传入一个被代理的对象
public ProxyFactory(Object target) {
this.target = target;
}
//返回一个代理对象,是target对象的代理对象
public Object getProxyInstance(){
//1.创建一个工具类
Enhancer enhancer = new Enhancer();
//2.设置父类
enhancer.setSuperclass(target.getClass());
//3.设置回调函数
enhancer.setCallback(this);
//4.创建子类对象,即代理对象
return enhancer.create();
}
//会调用目标对象的方法
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("Cglib代理模式开始");
Object returnVal = method.invoke(target, args);
System.out.println("Cglib代理模式提交");
return null;
}
}
package proxy.cglib;
public class Client {
public static void main(String[] args) {
//创建目标对象
TeacherDao target = new TeacherDao();
//获取代理对象,并将目标对象传递给代理对象
TeacherDao proxyInstance = (TeacherDao) new ProxyFactory(target).getProxyInstance();
//执行代理对象的方法,触发intecept方法,从而实现对目标对象的调用
proxyInstance.teach();
}
}
5.几种常见的代理模式介绍— 几种变体