小灰 程序员小灰





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




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








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

}








—————END—————



©著作权归作者所有:来自51CTO博客作者mob604756fef1ec的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. PyCharm激活码2021年5月份更新【亲测有效分享】
  2. 删除数组中对应的元素
  3. RTX51 Tiny 操作系统第二篇:任务的创建和删除
  4. Firefox logo仍包含小狐狸
  5. kubernetes环境宿主机异常关机导致重新部署harbor失败解决案例
  6. 定时删除10天前的Es索引
  7. (lintcode)第452题删除链表中的元素
  8. MySQL使用mysqldump+binlog完整恢复被删除的数据库
  9. 留言板实战与添加字数实时统计功能

随机推荐

  1. OMS:拥有Android血统的智能操作系统王
  2. Android中的异步消息处理机制Hander
  3. Android上的Back键事件捕获
  4. 如何判断用户用的是Android手机还是IOS手
  5. 2017 年 Android 领域大事件回顾
  6. Android自定义视图四:定制onMeasure强制显
  7. Android风格设计(style)
  8. Android中读取中文字符的文件与文件读取
  9. android java 判断某张表是否存在
  10. 如何学习android高级编程