MVC设计模式思想简述

  1. 什么是设计模式
    设计模式是一套被反复使用、多人知晓的,并经过分类编目的代码设计经验的总结。
    设计模式不是一种方法或技术,而是一种思想。
    语言无关、平台无关。例如:孙子兵法,三十六计等。

  2. 设计模式共23种,常用的4—6种
    工厂模式
    代理模式
    单例模式
    适配器模式
    MVC设计模式
    详见:https://blog.csdn.net/ljh0302/article/details/81562415

  3. MVC设计模式
    M—Model:模型层,用于数据封装(实体类)和业务逻辑(业务类或DAO类),主要通过JavaBean来实现。
    V—View:视图层,用于提供与用户交互的界面,动态展示数据,web项目中主要通过JSP来实现。客户端也可通过app方式实现。
    Controller:控制器层,用于处理请求并响应,主要通过Servlet来实现。

  4. MVC核心思想:分离。

  5. MVC的核心是控制器Controller,控制器的作用:
    接收请求参数
    控制业务逻辑(调用业务类)
    控制程序的流向(请求转发和重定向)

  6. 不能一个请求对应一个Servlet
    两种解决方案:
    方案一:

//以下是伪代码
Servlet{
				service(){
					if(operate.equals("aa")){
						处理aa请求
					}else if(operate.equals("bb")){
						处理bb请求
					}
					...
				}
				
			}

方案2:通过DispatchServlet实现“大C+小c模式,类似于层级管理模式”

//伪代码
			XxxServlet extends DispatchServlet{
				处理请求1();
				处理请求2();
				。。。
			}

DispatchServlet

public class DispatchServlet extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String flag=request.getParameter("flag");
		try {
			Method m=this.getClass().getDeclaredMethod(flag, HttpServletRequest.class,HttpServletResponse.class);//通过反射技术拼出方法
			m.invoke(this, request,response);//执行方法
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_37183032/article/details/88747577