SpringMVC adapter pattern code example

  • 2020-12-05 17:12:50
  • OfStack

The adapter pattern is adopted here. Due to the different types of Controller, there are multiple implementation modes, so the invocation mode is not determined. If the Controller method needs to be called directly, it needs to be written in the code as follows:


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

So suppose that if we add 1 HardController, we add 1 line of if(mappedHandler.getHandler () instanceof HardController) to the code
This form makes the program difficult to maintain and violates the open-close principle of design patterns -- open for extension and closed for modification.

Therefore, Spring defines one adapter interface, so that each kind of Controller has a corresponding adapter implementation class,
Let the adapter perform the corresponding method instead of controller. So when extending Controller, you only need to add one adapter class to complete the extension of SpringMVC, which is really a neat thing to do!

No more nonsense or on the code, in order to see clearly, on their own to achieve a set of code to simulate springMVC, Spring source paste easy to reduce concerns.


// define 1 a Adapter interface  
public interface HandlerAdapter {
	public Boolean supports(Object handler);
	public void handle(Object handler);
}
// The following is a 3 Kind of Controller implementation  
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...");
	}
}
// Let's write the 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);
	}
}
// simulation 1 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(){
		// The simulation SpringMVC from request take handler Object, only new Out, you can out,         
		// Whatever the implementation is Controller , the adapter can always be adapted to get the desired result  
		//   HttpController controller = new HttpController(); 
		//   AnnotationController controller = new AnnotationController(); 
		SimpleController controller = new SimpleController();
		// Get the corresponding adapter  
		HandlerAdapter adapter = getHandler(controller);
		// Execute the corresponding through the adapter controller Corresponding methods  
		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();
	}
}

This pattern can be seen in the open source code in the subtle, we need to have a goal when looking at the framework source code, so you will find a lot of things you need to learn, at present a lot of analysis of the source code posts are mostly about what, how,

I hope you can get up and talk about why.

conclusion

That's it for the SpringMVC adapter pattern code examples in this article, and I hope you found them helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: