漫画:什么是 “代理模式” ?

简介: 在上面的代码中,代理类和业务类继承了相同的接口,并且重写了添加/删除学生的方法。在重写的方法中,我们不仅可以调用业务类的原有方法,并且在调用的前后可以进行额外的处理,比如加上日志、事务等等。这样一来,在客户端当中,我们只要创建了代理类,就可以像使用业务类一样使用它,非常方便:

640.png640.png640.png640.png640.png640.png640.png

 

————————————

640.png640.png640.png640.png640.png640.png640.png640.png640.png640.png

 

public interface IStudentService {
    void insertStudent();
    void deleteStudent();
}
public class StudentService implements IStudentService {
    public void insertStudent(){
        //添加学生
    }
    public void deleteStudent(){
        //删除学生
    }
}

640.png640.png 

public class StudentService implements IStudentService {
    public void insertStudent(){
        System.out.println("准备添加学生");
        //添加学生
        System.out.println("添加学生成功");
    }
    public void deleteStudent(){
        System.out.println("准备删除学生");
        //删除学生
        System.out.println("删除学生成功");
    }
}

640.png640.png640.png640.png640.png640.png640.png640.png

public class StudentServiceProxy implements IStudentService {
    IStudentService studentService;
    public StudentServiceProxy(IStudentService studentService){
        this.studentService = studentService;
    }
    @Override
    public void insertStudent() {
        System.out.println("准备添加学生");
        studentService.insertStudent();
        System.out.println("添加学生成功");
    }
    @Override
    public void deleteStudent() {
        System.out.println("准备删除学生");
        studentService.deleteStudent();
        System.out.println("删除学生成功");
    }
}

 

在上面的代码中,代理类和业务类继承了相同的接口,并且重写了添加/删除学生的方法。

在重写的方法中,我们不仅可以调用业务类的原有方法,并且在调用的前后可以进行额外的处理,比如加上日志、事务等等。

这样一来,在客户端当中,我们只要创建了代理类,就可以像使用业务类一样使用它,非常方便:

public class Client {
    public static void main(String[] args) {
        IStudentService studentServiceProxy = new StudentServiceProxy(new StudentService());
        studentServiceProxy.insertStudent();
        studentServiceProxy.deleteStudent();
    }
}

640.png640.png640.png640.png640.png


Java语言为例,Java为我们提供了十分方便的创建动态代理的工具包。当我们生成动态代理的时候,我们需要使用到InvocationHandler接口和Proxy类。

具体的实现过程如下:




1.实现InvocationHandler接口,定义调用方法前后所做的事情:

public class StudentInvocationHandler implements InvocationHandler {
    private IStudentService studentService;
    public StudentInvocationHandler(IStudentService studentService){
        this.studentService = studentService;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName() + "方法调用前");
        method.invoke(studentService, args);
        System.out.println(method.getName() + "方法调用后");
        return null;
    }
}public class StudentInvocationHandler implements InvocationHandler {
    private IStudentService studentService;
    public StudentInvocationHandler(IStudentService studentService){
        this.studentService = studentService;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName() + "方法调用前");
        method.invoke(studentService, args);
        System.out.println(method.getName() + "方法调用后");
        return null;
    }
}

2.通过Proxy类的newProxyInstance方法,动态生成代理对象:

 

public class Client {
    public static void main(String[] args) {
        IStudentService studentService = new StudentService();
        InvocationHandler studentInvocationHandler = new StudentInvocationHandler(studentService);
        IStudentService studentServiceProxy = (IStudentService) Proxy.newProxyInstance(studentInvocationHandler.getClass().getClassLoader(), studentService.getClass().getInterfaces(), studentInvocationHandler);
        studentServiceProxy.insertStudent();
        studentServiceProxy.deleteStudent();
    }
}

640.png640.png640.png640.png640.png640.png


相关文章
|
5月前
|
设计模式 缓存 安全
小谈设计模式(8)—代理模式
小谈设计模式(8)—代理模式
|
5月前
|
设计模式
|
设计模式 缓存 安全
2023-6-29-第十一式代理模式
2023-6-29-第十一式代理模式
67 0
|
设计模式 缓存 Java
设计模式之代理模式(文末赠书)
设计模式之代理模式(文末赠书)
67 0
|
存储 设计模式 uml
漫画:设计模式中的 “观察者模式”
场景1:游戏操作界面 场景2:游戏迷宫
196 0
漫画:设计模式中的 “观察者模式”
|
设计模式 存储 缓存
【设计模式自习室】详解代理模式
《设计模式自习室》系列,顾名思义,本系列文章带你温习常见的设计模式。主要内容有: 该模式的介绍,包括: 引子、意图(大白话解释) 类图、时序图(理论规范) 该模式的代码示例:熟悉该模式的代码长什么样子 该模式的优缺点:模式不是万金油,不可以滥用模式 该模式的应用案例:了解它在哪些重要的源码中被使用
136 0
|
存储 设计模式 Java
漫画:什么是 “原型模式” ?
在Java语言中,Object类实现了Cloneable接口,一个对象可以通过调用Clone()方法生成对象,这就是原型模式的典型应用。 但需要注意的是,clone()方法并不是Cloneable接口里的,而是Object类里的,Cloneable是一个标识接口,标识这个类的对象是可被拷贝的,如果没有实现Cloneable接口,却调用了clone()方法,就会报错。
206 0
漫画:什么是 “原型模式” ?
漫画:什么是 “建造者模式” ?
首先,我们来定义一个Product类:接下来,我们定义抽象的Builder类:然后,是具体的Builder实现类:
120 0
漫画:什么是 “建造者模式” ?
|
设计模式 自动驾驶 Java
漫画设计模式:什么是 “装饰器模式” ?
装饰器模式都包含哪些核心角色呢? 1. Component接口 2. ConcreteComponent类 3. Decorator抽象类 4. ConcreteDecorator类
158 0
漫画设计模式:什么是 “装饰器模式” ?
|
设计模式
漫画:设计模式之 “工厂模式”
假设我们的业务代码当中,有一个被广泛引用的“口罩类”,这个类实例需要在许多地方被创建和初始化,而初始化的代码也比较复杂。
163 0
漫画:设计模式之 “工厂模式”