代理模式的基本介绍:
基本介绍: 静态代理在使用时,需要定义接口或者父类,被代理对象(即目标对象)与代理对象一起实现相同的接口或者是继承相同父类。
接口:
package com.atguigu.proxy.staticproxy;
public interface ITeacherDao {
public void teach();
}
被代理类:
package com.atguigu.proxy.staticproxy;
public class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("老师授课中!");
}
}
代理类:
package com.atguigu.proxy.staticproxy;
//代理对象,静态代理
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 com.atguigu.proxy.staticproxy;
public class Client {
public static void main(String[] args) {
//被代理对象
TeacherDao teacherDao=new TeacherDao();
//代理对象,将被代理对象传递给代理对象
TeacherDaoProxy teacherDaoProxy=new TeacherDaoProxy(teacherDao);
//通过代理对象调用被代理对象的实现方法
teacherDaoProxy.teach();
}
}
静态代理优缺点:
动态代理模式的基本介绍
JDK中生成代理对象的API
package com.atguigu.proxy.dynamic;
public interface ITeacherDao {
public void teach();
public void hello(String name);
}
目标类:
package com.atguigu.proxy.dynamic;
public class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("老师授课中!");
}
@Override
public void hello(String name) {
System.out.println("hello:"+name);
}
}
代理类:
package com.atguigu.proxy.dynamic;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory {
//维护一个目标对象
private Object target;
//构造器,对target进行初始化
public ProxyFactory(Object target) {
this.target = target;
}
public Object getProxyInstance(){
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);
System.out.println("JDK代理提交啦!");
return returnVal;
}
});
}
}
客户端:
package com.atguigu.proxy.dynamic;
public class Client {
public static void main(String[] args) {
ITeacherDao target=new TeacherDao();
ITeacherDao instance = (ITeacherDao) new ProxyFactory(target).getProxyInstance();
// System.out.println(instance);
instance.teach();
instance.hello("Emma");
}
}
Cglib代理模式的基本介绍
目标对象:
package com.itguigu.cglib;
public class TeacherDao {
public void teach() {
System.out.println("老师授课中!我是cglib代理对象,不需要实现接口。");
}
}
代理类:
package com.itguigu.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;
//构造器,对target进行初始化,传入被代理对象
public ProxyFactory(Object target) {
this.target = target;
}
//返回一个代理对象
public Object getProxyInstance(){
//创建一个工具类
Enhancer enhancer=new Enhancer();
//设置父类
enhancer.setSuperclass(target.getClass());
//设置回调函数
enhancer.setCallback(this);
//创建子类
return enhancer.create();
}
//重写方法,会调用目标函数的方法
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 returnVal;
}
}
客户端:
package com.itguigu.cglib;
public class Client {
public static void main(String[] args) {
TeacherDao target=new TeacherDao();
TeacherDao instance = (TeacherDao) new ProxyFactory(target).getProxyInstance();
instance.teach();
}
}
几种常见的代理模式介绍— 几种变体