1. 为什么要学习代理模式?
因为这就是SpringAOP的底层!【SpringAOP和SpringMVC)】
2. 代理模式的分类
- 静态代理
- 动态代理
3. 代理模式原型
4. 静态代理
4.1 角色分析
角色分析:
- 抽象角色:一般会使用接口或者抽象类来解决
- 真实角色︰被代理的角色
- 代理角色︰代理真实角色,代理真实角色后,我们一般会做一些附属操作
- 客户:访问代理对象的人!
案例:
- 接口
//租房
public interface Rent {
public void rent();
}
- 真实角色
//房东
public class House implements Rent {
public void rent() {
System.out.println("房东出租房子!");
}
}
- 代理角色
//中介
public class Proxy implements Rent {
public House house;
public Proxy() {
}
public Proxy(House house) {
this.house = house;
}
public void lookhouse() {
System.out.println("带租户看房子!");
}
public void rent() {
lookhouse();
this.house.rent();
heTong();
fare();
}
public void heTong() {
System.out.println("签合同");
}
public void fare() {
System.out.println("收取中介费");
}
}
- 客户端访问代理角色
public class Client {
public static void main(String[] args) {
House house = new House();
Proxy proxy = new Proxy(house);
proxy.rent();
}
}
代理模式的好处:
- 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
- 公共也就就交给代理角色!实现了业务的分工!
- 公共业务发生扩展的时候,方便集中管理!
缺点:
- 一个真实角色就会产生一个代理角色;
- 代码量会翻倍~ 开发效率会变低~
4.2 加深理解
4.2.1 原来的模式
UserService.java
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
UserServiceImpl.java
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("【Debug】执行了add方法");
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("【Debug】执行了delete方法");
System.out.println("删除了一个用户");
}
public void update() {`在这里插入代码片`
System.out.println("【Debug】执行了update方法");
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("【Debug】执行了query方法");
System.out.println("查询了一个用户");
}
}
测试
public class Client {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
userService.add();
}
}
弊端:
要是对每个方法添加新的业务逻辑则需要更改所有的代码。
4.2.2 利用Aop进行优化
UserService.java
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
UserServiceImpl.java
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("【Debug】执行了add方法");
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("【Debug】执行了delete方法");
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("【Debug】执行了update方法");
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("【Debug】执行了query方法");
System.out.println("查询了一个用户");
}
}
UserServiceProxy.java
public class UserServiceProxy implements UserService {
private UserServiceImpl userService;
public void setUserService(UserServiceImpl userService) {
this.userService = userService;
}
public void add() {
log("add");
System.out.println("增加了一个用户");
}
public void delete() {
log("delete");
System.out.println("删除了一个用户");
}
public void update() {
log("update");
System.out.println("修改了一个用户");
}
public void query() {
log("query");
System.out.println("查询了一个用户");
}
public void log(String msg) {
System.out.println("【Debug】执行了"+msg+"方法");
}
}
Client.java
public class Client {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
//userService.add();
UserServiceProxy proxy = new UserServiceProxy();
proxy.setUserService(userService);
proxy.add();
}
}
5. 动态代理
- 动态代理和静态代理角色一样
- 动态代理的代理类是动态生成的,不是我们直接写好的!
- 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
- 基于接口—JDK动态代理【我们在这里使用】
- 基于类: cglib
- java字节码实现: javasist
需要了解两个类: Proxy:代理,InvocationHandler:
5.1 租房案例
ProxyInvocationHandler.java
public class ProxyInvocationHandler implements InvocationHandler {
private Rent rent;
public void setRent(Rent rent) {
this.rent = rent;
}
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
lookhouse();
Object result = method.invoke(rent, args);
heTong();
fare();
return result;
}
public void lookhouse() {
System.out.println("带租户看房子!");
}
public void heTong() {
System.out.println("签合同");
}
public void fare() {
System.out.println("收取中介费");
}
}
测试
public class Client {
public static void main(String[] args) {
//真实角色
House house = new House();
//代理角色:现在没有
ProxyInvocationHandler pih = new ProxyInvocationHandler();
//通过调用程序处理角色来处理我们要调用的接口对象!
pih.setRent(house);
//这里的proxy就是动态生成的,我们并没有写!
Rent proxy = (Rent) pih.getProxy();
proxy.rent();
}
}
5.2 Service案例
ProxyInvocationHandler.java
public class ProxyInvocationHandler implements InvocationHandler {
private Object target;
public void setTarget(Object target) {
this.target = target;
}
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target, args);
return result;
}
}
测试:
public class Client {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
ProxyInvocationHandler pih = new ProxyInvocationHandler();
pih.setTarget(userService);
UserService proxy = (UserService) pih.getProxy();
proxy.add();
}
}
内容参考:https://www.bilibili.com/video/BV1WE411d7D
仅用于学习!