960x60
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

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

来源:本站原创 浏览:167次 时间:2021-05-24
小灰 程序员小灰





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




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








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—————



  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net