前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >漫画:什么是 “代理模式” ?

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

作者头像
小灰
发布2021-01-20 09:41:57
2460
发布2021-01-20 09:41:57
举报
文章被收录于专栏:程序员小灰程序员小灰

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

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

代码语言:javascript
复制
public interface IStudentService {
    void insertStudent();
    void deleteStudent();
}

public class StudentService implements IStudentService {

    public void insertStudent(){
        //添加学生
    }

    public void deleteStudent(){
        //删除学生
    }
}
代码语言:javascript
复制
public class StudentService implements IStudentService {

    public void insertStudent(){
        System.out.println("准备添加学生");
        //添加学生
        System.out.println("添加学生成功");
    }

    public void deleteStudent(){
        System.out.println("准备删除学生");
        //删除学生
        System.out.println("删除学生成功");
    }

}
代码语言:javascript
复制
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("删除学生成功");
    }
}

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

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

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

代码语言:javascript
复制
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接口,定义调用方法前后所做的事情:

代码语言:javascript
复制
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方法,动态生成代理对象:

代码语言:javascript
复制
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();
    }

}

—————END—————

喜欢本文的朋友,欢迎关注公众号 程序员小灰,收看更多精彩内容

代码语言:javascript
复制
点个[在看],是对小灰最大的支持!
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员小灰 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档