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

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

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


相关文章
|
Shell Linux C语言
【Shell 命令集合 磁盘管理 】Linux 改变当前工作目录 cd命令使用指南
【Shell 命令集合 磁盘管理 】Linux 改变当前工作目录 cd命令使用指南
1035 0
|
10月前
|
机器学习/深度学习 传感器 运维
数据不够用?别急着骂业务,数据增强和合成数据真能救命
数据不够用?别急着骂业务,数据增强和合成数据真能救命
472 2
|
8月前
|
数据采集 人工智能
OmniScience:大规模科学多模态数据集重磅上线
OmniScience是深势科技开源的科研图像理解数据集,含150万高质量“图-文-上下文”三元组、500万子图,覆盖10大科学领域。依托Uni-Parser与多模态大模型重描述,显著提升AI对科学图表的深层语义理解能力。
828 3
|
8月前
|
弹性计算 人工智能 运维
2026年阿里云价格最便宜云服务器实例规格、配置、价格与购买和抢购规则介绍
2026年阿里云推出多款高性价比入门级云服务器,包括轻量应用服务器和云服务器ECS特惠实例。轻量应用服务器2核2G配置38元/年起,2核4G配置9.9元/月起,适合个人开发者及小微企业。云服务器ECS特惠实例方面,经济型e实例2核2G配置99元/年,通用算力型u1实例2核4G配置199元/年,适合需要长期稳定运行的用户。用户可根据使用场景、技术能力及长期成本考量进行选择。
1119 0
|
9月前
|
人工智能 Java 测试技术
【内推专场】测试岗最高50K?拼多多/携程/阿里/网易,谁家最香?
本期精选拼多多、携程、网易、阿里及乌鸫科技6个一线大厂测试岗,覆盖上海、杭州。聚焦AI测试新趋势,网易、阿里等岗明确偏好AI经验;阿里系占3席,含金量高;薪资12K–50K,含16薪,部分岗2年经验即可投递。
|
9月前
|
监控 安全 Java
Java 代码修改:规范、技巧与避坑指南
Java 作为一门面向对象的高级编程语言,凭借跨平台、高安全性、强健壮性的特性,广泛应用于后端开发、大数据、安卓开发等领域
715 1
|
8月前
|
人工智能 算法 自动驾驶
AI伦理困境——算法偏见、隐私边界与监管博弈
随着AI系统深入渗透到社会运行的各个环节,伦理问题已从学术讨论变为现实危机。
991 0
|
10月前
|
机器学习/深度学习 弹性计算 编解码
阿里云4核8G云服务器ECS详解:ECS实例规格族、CPU 型号与适用场景全解析
阿里云4核8G ECS提供10余种实例规格,涵盖Intel/AMD/倚天多款CPU(如Xeon Sapphire Rapids、EPYC Genoa、倚天710),主频2.55–3.9 GHz。适用场景包括游戏前端、AI推理、视频编码、Web服务、大数据及科学计算等,兼顾性能与性价比。(239字)
1893 6
论这场云盘大战,以及各网盘的优劣
          最近几天,关于网盘的一场战役正如火如荼的展开。网盘市场应该是最近一年才兴起的。比较早的就是115网盘。后来随着金山快盘,百度云盘,以及google,华为,360等相继开发网盘业务,这个产业这在迅速崛起,我也是最近才有了试用网盘的习惯。          再来说说这几天的网盘大战,每个IT巨头都尽其所能。先是由金山快盘带动其的节奏,他们推出了100G永久免费。360云盘
2624 109