简介
由 Trygve Reenskaug 在1978年提出,是 Xerox PARC(施乐帕罗奥多研究中心)在20世纪80年代为程序语言 Smalltalk 发明的一种软件架构。
目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。除此之外,此模式透过对复杂度的简化,使程序结构更加直观。软件系统透过对自身基本部分分离的同时也赋予了各个基本部分应有的功能。
AngularJS/Ember/Django 就使用这种模式
M - Model
数据层 - 业务模型
Model represents the underlying, logical structure of data in a software application and the high-level class associated with it. This object model does not contain any information about the user interface.
- 封装与业务逻辑相关的数据
- 数据读取/处理/存储,提供数据接口
V - View
展示层 - 用户界面
View is a collection of classes representing the elements in the user interface (all of the things the user can see and respond to on the screen, such as buttons, display boxes, and so forth)
- 数据显示
- 动画效果
- 接受用户操作
C - Controller
控制器层 - M和V之间的连接器
Controller represents the classes connecting the model and the view, and is used to communicate between classes in the model and view
- 从 Model 获取数据更新 View
- 监听 View 的用户事件,处理与用户交互的逻辑
- 处理 View 的生命周期
- 处理界面之间的跳转
经典模型

View 接收用户事件,传递给 Controller 处理,Controller 更新 Model 数据,Model 改变后通知 View 更新,View 从 Model 获取数据显示
改良模型

View 接收用户事件,传递给 Controller 处理,Controller 更新 Model 数据,Model 改变后通知 Controller 更新 View,View 从 Controller 获取数据显示
实际使用中模型

Controller 持有 View,二者强耦合在一起和 Model 交互
优点
- 对长生命周期的项目的发展有帮助,可扩展性、可维护性高
- 可测试性,可对 Model 进行单元测试
- 多个 View 能共享一个 Model,提高了代码的可重用性
缺点
- 每个 View 的呈现都需要在一个 ViewController 基础上,每个 ViewController 都带有一个根 View,这就导致C和V紧密耦合在一起
- ViewController 很容易就会出现代码膨胀,变成 massive controller
- C的代码没法复用
- 视图对模型数据的低效率访问,视图可能需要多次调用才能获得足够的显示数据
参考资料
https://zh.wikipedia.org/wiki/MVC
https://www.jianshu.com/p/68aa6071c02e