本文共 4158 字,大约阅读时间需要 13 分钟。
职责链模式的作用是将请求从链中的一个对象传递到下一个对象,直到请求最终被响应处理为止。这种设计方式能够有效地去除对象之间的耦合,使系统更加灵活和可扩展。
在Servlet 2.3版本中,Filter和FilterChain接口被引入,其中FilterChain实现了职责链模式。Filter接口定义了一个doFilter方法,但如果链中没有其他Filter,则调用链将停止执行。下面是Filter和FilterChain的接口定义:
public interface Filter { void init(FilterConfig filterConfig) throws ServletException; void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException; void destroy();}
FilterChain接口仅定义了一个方法doFilter,用来控制下一个Filter的执行顺序。下面的代码展示了FilterChain的简单实现,称为VirtualFilterChain,它负责管理额外的Filter和原有的Filter链:
private static class VirtualFilterChain implements FilterChain { private final FilterChain originalChain; private final ListadditionalFilters; private int currentPosition = 0; public VirtualFilterChain(FilterChain chain, List additionalFilters) { this.originalChain = chain; this.additionalFilters = additionalFilters; } @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { if (this.currentPosition == this.additionalFilters.size()) { this.originalChain.doFilter(request, response); } else { this.currentPosition++; Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1); nextFilter.doFilter(request, response, this); } }}
这种实现方式允许开发者灵活地管理多个Filter,并确保它们按正确的顺序执行。
在Spring框架中,HandlerInterceptor和HandlerExecutionChain也采用了职责链模式。HandlerInterceptor定义了三个关键方法:
HandlerExecutionChain管理多个拦截器,并按特定顺序调用它们。以下是HandlerExecutionChain的关键实现代码:
public class HandlerExecutionChain { private HandlerInterceptor[] interceptors; private int interceptorIndex; public void applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { if (!this.interceptors Empty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; } public void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception { if (!this.interceptors Empty(interceptors)) { for (int i = interceptors.length - 1; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; interceptor.postHandle(request, response, this.handler, mv); } } } public void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception { if (!this.interceptors Empty(interceptors)) { for (int i = this.interceptorIndex; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; try { interceptor.afterCompletion(request, response, this.handler, ex); } catch (Throwable ex2) { logger.error("HandlerInterceptor.afterCompletion threw exception", ex2); } } } }}
DispatcherServlet在调用HandlerAdapter之前,会调用HandlerExecutionChain的applyPreHandle方法,并在响应生成后调用applyPostHandle方法。需要注意的是,如果applyPreHandle返回false,会触发afterCompletion方法处理异常情况。
以下是一些经典的Java学习资源推荐:
以下是一些常见的Java面试题及其答案:
通过上述案例,可以清晰地看到职责链模式在不同框架中的应用和优势。在实际开发中,它能够有效地降低代码耦合度,提高系统的扩展性和可维护性。
转载地址:http://fgaxz.baihongyu.com/