In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to understand the adapter pattern in the web design pattern". Many people will encounter this dilemma in the operation of the actual case, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Adapter pattern (Adapter Pattern): converts one interface to another that the customer wants, and the adapter pattern allows classes with incompatible interfaces to work together, aliased as Wrapper. The adapter pattern can be used as either a class structural pattern or an object structural pattern.
Pattern structure
The adapter pattern contains the following roles:
Target: target abstract class
Adapter: adapter class
Adaptee: adaptor class
Client: customer CLA
Source code guide
We all know that springMVC uses the adapter pattern, so how does it adapt? let's take a look at its source code. First of all, we need to understand the implementation principle of springMVC. I don't like to describe the whole process here. Let's talk about the key parts:
DispatcherServlte registers HandlerAdapter according to the configuration file information. If there is no configuration in the configuration file, DispatcherServlte will get the default configuration of HandlerAdapter. If you read the default configuration, DispatcherServlte will read the DispatcherServlte.properties file, which has three HandlerAdapter:HttpRequestHandlerAdapter,SimpleControllerHandlerAdapter and AnnotationMethodHandlerAdapter configured. DispatcherServlte stores the three HandlerAdapter objects in its handlerAdapters collection property, which completes the registration of the HandlerAdapter.
DispatcherServlte will match the controller passed by handlerMapping with the registered HandlerAdapter one by one to see which HandlerAdapter supports this controller type. If one of the HandlerAdapter is found to support the passed controller type, then the HandlerAdapter will call its own handle method, and the handle method uses the reflection mechanism of java to execute the specific method of controller to obtain ModelAndView.
DispatcherServlte partial source code
Public class DispatcherServlet extends FrameworkServlet {. . @ Nullable private List handlerMappings; @ Nullable private List handlerAdapters; protected void doDispatch (HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; boolean multipartRequestParsed = false; WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager (request); try {try {ModelAndView mv = null; Object dispatchException = null Try {. . HandlerAdapter ha = this.getHandlerAdapter (mappedHandler.getHandler ()); . Mv = ha.handle (processedRequest, response, mappedHandler.getHandler ()); if (asyncManager.isConcurrentHandlingStarted ()) {return;} this.applyDefaultViewName (processedRequest, mv); mappedHandler.applyPostHandle (processedRequest, response, mv);} catch (Exception var20) {dispatchException = var20 } catch (Throwable var21) {. }. } catch (Exception var22) {. } catch (Throwable var23) {. }} finally {. . }}}
Only the key code is put here, and we can see that when a request enters the doDispatch () method, it first goes to getHandlerAdapter () to get the adapter, which is the second step to find the corresponding adapter according to the controller in handlerMapping. After finding the adapter, executing our own controller,mappedHandler.getHandler () via ha.handle (processedRequest, response, mappedHandler.getHandler ()) is our own controller.
As for how handler () knows which method to execute in controller, of course it is through annotations to transform the corresponding method. Therefore, the adapter pattern here is not particularly pure, but also combines the reflection mechanism. DispatcherServlte belongs to the client, our Controller belongs to the adapted class, and HandlerAdapter belongs to the adapter.
Now let's assume that we need to write a thread pool task scheduling framework. We know that JDK's native thread framework can create a thread pool, but thread pools can only be passed in objects that implement the runnable interface or the callable interface.
ExecutorService cachedThreadPool = Executors.newCachedThreadPool (); cachedThreadPool.execute (new Runnable () {@ Override public void run () {}})
So how can we make the client use this framework without inheriting runnable. You can use adapters to annotate like springMVC. You can also provide an abstract adapter class that implements the Runnable interface, allowing the client to configure to adapt the normal class to Runnable.
There are many other aspects about the use of adapters, such as WebSecurityConfigurerAdapter in spring security and ChannelInboundHandlerAdapter in netty generally end with Adapter for adapter pattern class names
That's all for "how to understand the Adapter pattern in the web Design pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.