来简单实现springmvc
先来2个接口,Adapter--[关联]--> Controller
public interface Controller {
//抽象的业务方法 处理http请求和响应 并返回 ModelAndView
//由不同的实现类去实现
ModelAndView handleRequest(String request, String response) throws Exception;
}
public interface HandlerAdapter {
//检查
public boolean supports(Object handler);
//具体的处理业务方法 关联 Controller
public void handle(Controller handler);
}
Controller 的不同实现
public class HttpController implements Controller {
@Override
public ModelAndView handleRequest(String request, String response) throws Exception {
return null;
}
public void doHttpHandler(){
System.out.println("http...");
}
}
public class AnnotationController implements Controller {
@Override
public ModelAndView handleRequest(String request, String response) throws Exception {
return null;
}
public void doAnnotationHandler(){
System.out.println("annotation...");
}
}
public class SimpleController implements Controller {
@Override
public ModelAndView handleRequest(String request, String response) throws Exception {
return null;
}
public void doSimplerHandler(){
System.out.println("simple...");
}
}
adapter的实现
public class HttpHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object controller) {
return controller instanceof HttpController;
}
@Override
public void handle(Controller controller) {
((HttpController) controller).doHttpHandler();
}
}
public class AnnotationHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object controller) {
return controller instanceof AnnotationController;
}
/**
* 传入具体的 controller的实现 AnnotationController
*
* @param controller 引用占位-实际是具体的实现类
*/
@Override
public void handle(Controller controller) {
//技巧:强转
((AnnotationController) controller).doAnnotationHandler();
}
}
public class SimpleHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object controller) {
return controller instanceof SimpleController;
}
@Override
public void handle(Controller controller) {
//强转
((SimpleController) controller).doSimplerHandler();
}
}
DispatchServlet--[关联]-->adapter
/**
* @desc 关联 adapter --> List<HandlerAdapter>
*/
public class FlynnDispatchServlet {
//类型是 HandlerAdapter 接口 来注入HandlerAdapter的实现类
private List<HandlerAdapter> handlerAdapters;
public FlynnDispatchServlet() {
this.handlerAdapters = new ArrayList<>();
handlerAdapters.add(new AnnotationHandlerAdapter());
handlerAdapters.add(new HttpHandlerAdapter());
handlerAdapters.add(new SimpleHandlerAdapter());
}
void doDispatch(int type) {
Controller controller;
HandlerAdapter handlerAdapter;
//动态实现的地方
switch (type) {
case 1:
controller = new HttpController();
handlerAdapter = getHandlerFromController(controller);
break;
case 2:
controller = new AnnotationController();
handlerAdapter = getHandlerFromController(controller);
break;
default:
controller = new SimpleController();
handlerAdapter = getHandlerFromController(controller);
break;
}
if (null == handlerAdapter) {
return;
}
handlerAdapter.handle(controller);
}
private HandlerAdapter getHandlerFromController(Controller controller) {
for (HandlerAdapter adapter : this.handlerAdapters) {
if (adapter.supports(controller)) {
return adapter;
}
}
return null;
}
public class AppSpringMvc {
public static void main(String[] args) {
FlynnDispatchServlet dispatchServlet = new FlynnDispatchServlet();
dispatchServlet.doDispatch(1);
dispatchServlet.doDispatch(2);
dispatchServlet.doDispatch(9);
}
}
输出