代理设计模式
2021-06-01 20:55:12
### 代理
#### 代理设计模式
使用一个代理类来代替另一个类,从而达到新增功能等.
##### 静态代理
代理类是自己手动创建,不是动态创建
案例如下
###### UserService
被代理类接口,代理类和被代理类都需要实现该接口
```java
public interface UserService {
void insert();
void delete();
void update();
void find();
}
```
###### UserServiceImpl
被代理的类
```java
public class UserServiceImpl implements UserService{
public void insert() {
System.out.println("增加一个user");
}
public void delete() {
System.out.println("删除一个user");
}
public void update() {
System.out.println("更新一个user");
}
public void find() {
System.out.println("查找一个user");
}
}
```
UserServiceProxy
代理类,代理UserServiceImpl
```java
public class UserServiceProxy implements UserService {
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void insert() {
before();
userService.insert();
after();
}
public void delete() {
before();
userService.delete();
after();
}
public void update() {
before();
userService.update();
after();
}
public void find() {
before();
userService.find();
after();
}
public void before(){
System.out.println("前日志");
}
public void after(){
System.out.println("后日志");
}
}
```
###### 测试类Test
```java
public class Test {
public static void main(String[] args) {
UserServiceProxy proxy = new UserServiceProxy();
proxy.setUserService(new UserServiceImpl());
proxy.insert();
proxy.update();
proxy.delete();
proxy.find();
}
}
```
##### 动态代理
动态代理底层是反射的知识,Java代理类相关
动态代理是指代理类是根据特定需求动态生成的,一个接口/一类功能只需要一个Handler方法生产代理类
案例如下:
###### Student
被代理接口
```java
public interface Student {
String getName();
void setName(String name);
}
```
###### StudentImpl
被代理的类
```java
public class StudentImpl implements Student{
private String name;
public StudentImpl(String name) {
this.name = name;
}
public StudentImpl() {
}
public String getName() {
System.out.println("getName");
return "hello" + name;
}
public void setName(String name) {
System.out.println("setName");
this.name = name;
}
private void test(){
System.out.println("test");
}
}
```
###### LogHandler
生成代理类的“工具类”
```java
public class LogHandler implements InvocationHandler {
private Object object;
public LogHandler(Object object) {
this.object = object;
}
public void setObject(Object object) {
this.object = object;
}
public Proxy getProxy(){
return (Proxy) Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
beforeLog(method.getName());
System.out.println("参数:");
for(Object obj : args){
System.out.println(obj.toString());
}
Object o = method.invoke(object,args);
afterLog(method.getName());
return o;
}
//插入的方法
public void beforeLog(String message){
System.out.println(message+"方法的前日志");
}
public void afterLog(String message){
System.out.println(message+"方法的后日志");
}
}
```
###### Test测试类
```java
public class Test {
public static void main(String[] args) {
LogHandler logHandler = new LogHandler(new StudentImpl("张三"));
Student proxy = (Student) logHandler.getProxy();
proxy.setName("hehe");
}
}
```