在 《 企业应用架构模式 》这本书中,它只有提到 MVC 这个它放在 Presentation 的模式,而接下来的几篇文章的我们会多加了两个模式分别为『 MVVM 』与 『 MVP 』,接下来这篇文章我们要好好的来理解,MVC 最原始的意思。
MVC 最早基本上应该是属於 presentation,它主要聚焦在画面根据 model 的互动,它最重要的概念在於将 UI 与 model 分离出来,它的组成如下 :
传统上它後他们的关系如下图,这里应该会有人觉得很奇怪,为什麽 view 会与 model 有连结,应该有不少人使用是以 Backend 角度来想,那的确没错,因为例如 asp.net mvc 应该是 view → controller → model,而 model 不会与 view 有关系。
图片来源: educative-Acing the JavaScript Design Patterns Interview
我当初看到这张图有是有这想法,因此我去查了一下最原始的 MVC 出在《 A Description of the Model-View-Controller User Interface Paradigm in the Smalltalk-80 System 》这篇论文,它最原始的图的确 model 会与 view 相互沟通,如下图 :
图片来源: 《 A Description of the Model-View-Controller User Interface Paradigm in the Smalltalk-80 System 》
我觉得有两点,首先第一点在於『 传统 MVC 应该是 view → controller → model,而 model 与 view 并不会有沟通 』,以 asp.net mvc 来看确是上面这样,但我觉得当初会这样做有两个原因,第一点在於 web 中间多了个 http,而不像原始的 MVC 是设计上一般 gui 工具上。第二点当初我觉得 ASP.NET MVC 设计初宗是为了提现『 分离 』的概念。
然後第二点混乱点在於, 与我们上一篇文章提到的 3-Tier 搞混,MVC 是聚焦画面与资料的互动模式,这也是为什麽 《 企业应用架构模式 》这本书作者会放在 presentation 层级,而 3-Tier 主要是聚焦在整个软件架构上。
这些混乱的讨论我觉得可以看一下,这篇文章的留言区,就会知道每个人的 MVC 真的完全不一样。
https://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html
我这里的范例,比较这张图,因为原始论文 view 对 controller 的行为互动没连线,但下面这张又缺了 model 对 controller 的变化通知,但论文里的范例码是有关系的。
图片来源: educative-Acing the JavaScript Design Patterns Interview
主要分成三个部份 :
这里有几个重点要注意 :
const EventEmitter = require('events');
class UserView{
init(controller, model){
this.controller = controller
this.model = model
const that = this
this.model.on('model-change', () => {
that.updateView()
})
}
registerUser(name){
this.controller.registerUser(name)
}
updateView(){
console.log('Update user view !!!')
}
}
class UserListView{
init(controller, model){
this.controller = controller
this.model = model
const that = this
this.model.on('model-change', () => {
that.updateView()
})
}
updateView(){
console.log('Update user list view !!!')
}
}
class UserModel extends EventEmitter{
create(name){
console.log(`Create a user#${name}`)
this.emit('model-change')
}
}
class UserController{
init(userView, model){
this.view = userView
this.model = model
}
registerUser(name){
this.model.create(name)
}
}
const userModel = new UserModel()
const userView = new UserView()
const userListView = new UserListView()
const userController = new UserController()
userView.init(userController, userModel)
userListView.init(userController, userModel)
userController.init(userView, userModel)
userView.registerUser('Mark')
// Create a user#Mark
// Update user view !!!
// Update user list view !!!
这篇文章的范例是我主要根据最原始论文的内容,所撰写的我自已觉得最初的概念是这样的程序码,不代表一定正确 ~ 请自行判断 ~ 毕竟 MVC 这个内容如果你有认真去寻找相关资料,应该真的会很混乱 ~ 我也混乱很久,所以我才去找最原始版本的来看看 ~
MVC 会出来,我觉得最主要就是因为 SRP 单一职责原则,所提出的,定义好每部份 ( M、V、C ) 要做什麽事情。
ASP.NET MVC 我真一直以为最原始的 mvc 就是它的样子,後来才发现它算是改良过的,不如说我觉得比较接近 MVP 啊。
前言 今天会继续探讨其他的 flask 的函式。如果说昨天的函式是用来给予使用者回应,那今天提到的函...
python 条件式 if,注意 python run code 是 依据 code 排版 ,前後顺...
使用运行Windows 10的计算机时,您可能会得到「此Windows版本即将过期。Windows版...
今天子标题是Network & Port scanner,其实跟前面介绍的几个工具功能好像有...
Javascript 中将资料型态分成,基本型别(Primitive)与物件型别(Object)两大...