feat: 代理模式
This commit is contained in:
parent
12f5f6600c
commit
7757a72122
Binary file not shown.
After Width: | Height: | Size: 6.7 KiB |
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
|
@ -1360,7 +1360,100 @@ public class Composite {
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 代理模式---结构型3
|
## 代理模式---结构型
|
||||||
|
|
||||||
|
### 静态代理
|
||||||
|
|
||||||
|
![image-20250204185312828](./images/设计模式-v2/image-20250204185312828.png)
|
||||||
|
|
||||||
|
如果是静态代理,做Java开发几乎都在用,就是抽象接口和实现,比如:`UserService`之后`UserServiceImpl`实现`UserService`,就是这个。
|
||||||
|
|
||||||
|
之后使用`UserService`进行调用实现中的方法,所以做过后端开发这个没有什么必要说的;真要说就是动态代理。
|
||||||
|
|
||||||
|
### 动态代理
|
||||||
|
|
||||||
|
![image-20250204185852710](./images/设计模式-v2/image-20250204185852710.png)
|
||||||
|
|
||||||
|
#### 示例代码
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Proxy {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
UserServiceProxy proxy = new UserServiceProxy(new UserServiceImpl());
|
||||||
|
proxy.saveUser();
|
||||||
|
proxy.findUser(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
void saveUser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*/
|
||||||
|
Object findUser(Long id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void saveUser() {
|
||||||
|
System.out.println("保存用户");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*
|
||||||
|
* @param id id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object findUser(Long id) {
|
||||||
|
System.out.println("查找用户");
|
||||||
|
return "User{name:'代理'}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class UserServiceProxy implements UserService {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
public UserServiceProxy(UserService userService) {
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void saveUser() {
|
||||||
|
userService.saveUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*
|
||||||
|
* @param id id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object findUser(Long id) {
|
||||||
|
return userService.findUser(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 运行结果:
|
||||||
|
|
||||||
|
```java
|
||||||
|
保存用户
|
||||||
|
查找用户
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package cn.bunny.pattern17;
|
||||||
|
|
||||||
|
public class Proxy {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
UserServiceProxy proxy = new UserServiceProxy(new UserServiceImpl());
|
||||||
|
proxy.saveUser();
|
||||||
|
proxy.findUser(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
void saveUser();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*/
|
||||||
|
Object findUser(Long id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void saveUser() {
|
||||||
|
System.out.println("保存用户");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*
|
||||||
|
* @param id id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object findUser(Long id) {
|
||||||
|
System.out.println("查找用户");
|
||||||
|
return "User{name:'代理'}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class UserServiceProxy implements UserService {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
public UserServiceProxy(UserService userService) {
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存用户
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void saveUser() {
|
||||||
|
userService.saveUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找用户
|
||||||
|
*
|
||||||
|
* @param id id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Object findUser(Long id) {
|
||||||
|
return userService.findUser(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue