静态
手动编写代理类进行代理,通常代理类和目标类都实现同一接口
// 定义接口
interface Image {
void display();
}
// 创建被代理类
class RealImage implements Image {
private String filename;
public RealImage(String filename) {
this.filename = filename;
loadFromDisk(); // 模拟加载图片的过程
}
private void loadFromDisk() {
System.out.println("Loading image: " + filename);
}
public void display() {
System.out.println("Displaying image: " + filename);
}
}
// 创建代理类
class ImageProxy implements Image {
private String filename;
private RealImage realImage;
public ImageProxy(String filename) {
this.filename = filename;
}
public void display() {
if (realImage == null) {
realImage = new RealImage(filename);
}
realImage.display();
}
}
// 测试代理模式
public class ProxyPatternExample {
public static void main(String[] args) {
Image image = new ImageProxy("image.jpg");
// 第一次调用display(),会创建实际的图片对象并显示
image.display();
// 第二次调用display(),直接使用已经创建的图片对象并显示
image.display();
}
}
动态
编写一个实现 InvocationHandler接口的处理器类来代理,创建代理对象,代理对象执行代理返回实体的对象
// 定义接口
interface Subject {
void doAction();
}
// 创建被代理类
class RealSubject implements Subject {
public void doAction() {
System.out.println("RealSubject is doing action.");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
// 创建处理器类
class ProxyHandler implements InvocationHandler {
private Object realSubject;
public ProxyHandler(Object realSubject) {
this.realSubject = realSubject;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 执行代理逻辑
System.out.println("Proxy is doing something before the real subject.");
Object result = method.invoke(realSubject, args);
System.out.println("Proxy is doing something after the real subject.");
return result;
}
}
import java.lang.reflect.Proxy;
// 测试动态代理
public class DynamicProxyExample {
public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
ProxyHandler handler = new ProxyHandler(realSubject);
// 创建代理对象
Subject proxySubject = (Subject) Proxy.newProxyInstance(
realSubject.getClass().getClassLoader(),
realSubject.getClass().getInterfaces(),
handler);
// 调用代理对象的方法,触发代理逻辑
proxySubject.doAction();
}
}
CGLIB
编写一个实现MethodInterceptor接口的处理器类
使用setSuperclass方法让代理类成为实体类的父类,再使用setCallback回调已经设置好的对象
public class RealSubject {
public void doAction() {
System.out.println("RealSubject is doing action.");
}
}
public class ProxyHandler implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// 执行代理逻辑
System.out.println("Proxy is doing something before the real subject.");
Object result = proxy.invokeSuper(obj, args);
System.out.println("Proxy is doing something after the real subject.");
return result;
}
}
public class CglibProxyExample {
public static void main(String[] args) {
ProxyHandler handler = new ProxyHandler();
// 创建代理对象
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(RealSubject.class);
enhancer.setCallback(handler);
RealSubject proxySubject = (RealSubject) enhancer.create();
// 调用代理对象的方法,触发代理逻辑
proxySubject.doAction();
}
}