1.什么是代理?
代理模式是一种结构型设计模式,代理可以对被代理对象进行增强,同时又可以将被代理对象的具体实现与客户端代码分离
2.实现步骤
静态代理:在编译期间就已经确定了代理的实现方式,它要求代理对象和被代理对象都实现同一接口或继承同一个父类
/**
* @author LJL
* @version 1.0
* @title IUserService
* @date 2023/6/9 16:36
* @description 用户接口
*/
public interface IUserService {
void login(String name, String password);
}
/**
* @author LJL
* @version 1.0
* @title UserServiceImpl
* @date 2023/6/9 16:37
* @description 用户接口的实现类
*/
public class UserServiceImpl implements IUserService {
@Override
public void login(String name, String password) {
System.out.println(name + " login with password: " + password);
}
}
/**
* @author LJL
* @version 1.0
* @title UserServiceProxy
* @date 2023/6/9 16:38
* @description 用户接口的代理类
*/
public class UserServiceProxy implements IUserService {
private IUserService target;
public UserServiceProxy(IUserService target) {
this.target = target;
}
@Override
public void login(String name, String password) {
if (password.length() < 8) {
System.out.println("password too short!");
return;
}
target.login(name, password);
}
}
public class Test {
public static void main(String[] args) {
IUserService userService = new UserServiceImpl();
IUserService userServiceProxy = new UserServiceProxy(userService);
userServiceProxy.login("张三", "1234567"); // password too short!
userServiceProxy.login("李四", "12345678"); // 李四 login with password: 12345678
}
}
动态代理:而是在运行期间生成字节码,动态地生成代理类。通过 JDK、CGLib 等技术实现
/**
* @author LJL
* @version 1.0
* @title Calculator
* @date 2023/6/9 16:43
* @description 被代理对象实现的接口
*/
public interface Calculator {
int add(int a, int b);
}
/**
* @author LJL
* @version 1.0
* @title CalculatorImpl
* @date 2023/6/9 16:44
* @description 动态被代理实现类
*/
public class CalculatorImpl implements Calculator {
@Override
public int add(int a, int b) {
return a + b;
}
}
/**
* @author LJL
* @version 1.0
* @title CalculatorProxy
* @date 2023/6/9 16:45
* @description 动态代理对象
*/
public class CalculatorProxy implements InvocationHandler {
private Object target;
public CalculatorProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before method " + method.getName() + " invoked");
Object result = method.invoke(target, args);
System.out.println("result : " + result);
System.out.println("after method " + method.getName() + " invoked");
return result;
}
}
/**
* @author LJL
* @version 1.0
* @title Test
* @date 2023/6/9 16:40
* @description 测试代码
*/
public class Test {
public static void main(String[] args) {
Calculator calculator = new CalculatorImpl();
/**
* calculator.getClass().getClassLoader() 被代理对象类加载器
* new Class[]{Calculator.class} 被代理对象接口
* new CalculatorProxy(calculator) 代理对象的实现类
*/
Calculator proxy = (Calculator) Proxy.newProxyInstance(calculator.getClass().getClassLoader(),
new Class[]{Calculator.class}, new CalculatorProxy(calculator));
System.out.println(proxy.add(1, 2));
}
}
3.适用场景
鉴权、缓存、事务管理、异常处理、日志
4.总结
- 静态代理不够灵活,如果需要增加新的代理对象或被代理对象,都需要重新写新的代理类,比较繁琐。
- 如果被代理对象的方法较多,需要实现很多的代理方法,增加开发和维护的难度和工作量。
- 动态代理抽象出接口实现,与具体的业务实现解耦,更容易维护和扩展。同时增加和删除被代理类也更加方便。
- 动态代理实现更加简洁,不需要手动编写大量的代理类。