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

本文介绍Java代理模式的应用,包括静态代理和动态代理的实现。通过示例代码展示了如何利用代理模式增强类的功能,如添加日志和事务处理。

作者 | 小灰

来源 | 程序员小灰(ID:chengxuyuanxiaohui)



—————  第二天  —————



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



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




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






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();    }}



以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;    }}

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();    }
}




更多精彩推荐
☞必看!程序员逃生指南
☞“硅谷之父”传奇:拯救斯坦福大学、培养大批高科技人才、指导创立惠普
☞腾讯、火绒回应“QQ 读取浏览器历史记录”;字节跳动起诉百度获赔 40 元;Redis 6.0.10 发布 | 极客头条

☞云原生体系下的技海浮沉与理论探索

☞最新组合式模型量化方法,实现FPGA最高硬件利用率,准确率-推理速度达到SOTA
☞引介 | 用大白话解释 Taproot 对隐私性的影响

点分享

点收藏

点点赞

点在看

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值