SpringMVC adapter pattern code example, springmvc Adapter

Source: Internet
Author: User

SpringMVC adapter pattern code example, springmvc Adapter

The adapter mode is used here. Due to different Controller types and multiple implementation methods, the calling method is not definite. If you need to call the Controller method directly, you need to write the following form in the Code:

if(mappedHandler.getHandler() instanceof MultiActionController){   ((MultiActionController)mappedHandler.getHandler()).xxx }else if(mappedHandler.getHandler() instanceof XXX){   ... }else if(...){   ... }

In this case, if we add a HardController, we need to add a line of if (mappedHandler. getHandler () instanceof HardController) to the code)
This form makes the program difficult to maintain, and violates the open and closed principle in the design mode-open to the extension and close the modification.

Therefore, Spring defines an adaptation interface so that each Controller has a corresponding adapter implementation class,
Let the adapter execute the corresponding method instead of the controller. In this way, when you expand the Controller, you only need to add an adapter class to complete the SpringMVC extension. This is really an exquisite practice!

Let's talk about the code. To make it clear, implement a set of code to simulate springMVC and directly paste the Spring source code to reduce the focus.

// Define an Adapter interface public interface HandlerAdapter {public Boolean supports (Object handler); public void handle (Object handler );} // The following three controllers implement public interface Controller {} public class HttpController implements Controller {public void doHttpHandler () {System. out. println ("http... ") ;}} public class SimpleController implements Controller {public void doSimplerHandler () {System. out. println ("simple... ") ;}} public class AnnotationController implements Controller {public void doAnnotationHandler () {System. out. println ("annotation... ") ;}} // write the following adapter class public class SimpleHandlerAdapter implements HandlerAdapter {public void handle (Object handler) {(SimpleController) handler ). doSimplerHandler ();} public Boolean supports (Object handler) {return (handler instanceof SimpleController);} public class HttpHandlerAdapter implements HandlerAdapter {public void handle (Object handler) {(HttpController) handler ). doHttpHandler ();} public Boolean supports (Object handler) {return (handler instanceof HttpController);} public class AnnotationHandlerAdapter implements HandlerAdapter {public void handle (Object handler) {(AnnotationController) handler ). doAnnotationHandler ();} public Boolean supports (Object handler) {return (handler instanceof AnnotationController);} // simulate a DispatcherServlet import java. util. arrayList; import java. util. list; public class DispatchServlet {public static List <HandlerAdapter> handlerAdapters = new ArrayList <HandlerAdapter> (); public DispatchServlet () {handlerAdapters. add (new AnnotationHandlerAdapter (); handlerAdapters. add (new HttpHandlerAdapter (); handlerAdapters. add (new SimpleHandlerAdapter ();} public void doDispatch () {// simulate SpringMVC to retrieve the handler object from the request, only new, can be output, // no matter what Controller is implemented, the adapter can always get the desired result after adaptation // HttpController controller = new HttpController (); // AnnotationController controller = new AnnotationController (); simpleController controller = new SimpleController (); // obtain the corresponding adapter HandlerAdapter adapter = getHandler (controller); // use the adapter to execute the corresponding controller Method adapter. handle (controller);} public HandlerAdapter getHandler (Controller controller) {for (HandlerAdapter adapter: this. handlerAdapters) {if (adapter. supports (controller) {return adapter;} return null;} public static void main (String [] args) {new DispatchServlet (). doDispatch ();}}

Through this mode, we can see the subtlety of the open source code. when looking at the source code of the framework, we need to look at the objective, so we can find a lot of things we need to learn, at present, most of the posts on source code analysis are about what, how,

I hope you can discuss "why" together"

Summary

The above is all the content about the SpringMVC adapter pattern code sample in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.