第七章 适配器模式 Adapter
结构型设计模式
定义:
将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间
类图:
应用:
SpringMVC中的适配器模式,在SpringMVC中,使用DispatcherSevlet拦截所有请求,再根据不同的uri调用对应的服务方法。DispatcherSevlet中最核心的doDispatch()方法,截取一段分发服务的代码,这里就用到了适配器模式
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { ... try { ModelAndView mv = null; Exception dispatchException = null; try { processedRequest = checkMultipart(request); multipartRequestParsed = (processedRequest != request); // Determine handler for the current request. mappedHandler = getHandler(processedRequest); if (mappedHandler == null) { noHandlerFound(processedRequest, response); return; } // Determine handler adapter for the current request. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); // Process last-modified header, if supported by the handler. String method = request.getMethod(); boolean isGet = HttpMethod.GET.matches(method); if (isGet || HttpMethod.HEAD.matches(method)) { long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } ... }
首先我们来看这个适配器接口(HandlerAdapter )
public interface HandlerAdapter { boolean supports(Object var1); @Nullable ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception; long getLastModified(HttpServletRequest var1, Object var2); }
HandlerAdapter接口约束了他的子类必须实现的三个方法,判断是否支持这个handler,执行handler并返回ModelAndView对象,获取最近一次修改时间。很明显这个HandlerAdapter就是为了针对不同的服务实现返回同样的视图。这里,需要了解一下SpringMVC中有多少种服务的实现方式,看一下下面的实现类:
最典型的三个:
- 使用@RequestMapping修饰的方法。这里就是使用RequestMappingHandlerAdapter,去处理这个服务请求,想知道细节的可以去源码看
- 实现了Servlet接口。这里就是使用SimpleServletHandlerAdapter
public class SimpleServletHandlerAdapter implements HandlerAdapter { @Override public boolean supports(Object handler) { return (handler instanceof Servlet); } @Override @Nullable public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { ((Servlet) handler).service(request, response); return null; } @Override public long getLastModified(HttpServletRequest request, Object handler) { return -1; } }
- 实现了Controller接口
这里就是使用SimpleControllerHandlerAdapter
public class SimpleControllerHandlerAdapter implements HandlerAdapter { @Override public boolean supports(Object handler) { return (handler instanceof Controller); } @Override @Nullable public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return ((Controller) handler).handleRequest(request, response); } @Override public long getLastModified(HttpServletRequest request, Object handler) { if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); } return -1L; } }
Java I/O中适配器模式:
InputStreamReader/OutputStreamWriter
不积跬步,无以至千里;不积小流,无以成江海