模型、 View 和 Controller 之间的关系让我感到困惑。
本主题显示了从 View 到 Controller 的箭头、从 Controller 到模型的箭头以及从模型到 View 的箭头:
http://www.codeproject.com/Tips/31292/MVC-v-s-MVP-How-Common-and-How-Different
但是,本主题显示了模型和 View 之间的双箭头; View 和 Controller 之间的双箭头;以及从 Controller 到模型的箭头:
http://www.codeproject.com/Articles/288928/Differences-between-MVC-and-MVP-for-Beginners
最后,本主题显示了从 View 到模型的箭头、从 Controller 到模型的箭头以及从 Controller 到 View 的箭头:
http://www.w3schools.com/aspnet/mvc_intro.asp
我有一些问题:
最佳答案
我发现您链接到的所有图像都令人困惑。这张图片 ( taken from Wikipedia ) 解释得最好。
How It Works
MVC considers three roles. The model is an object that represents some information about the domain. It's a nonvisual object containing all the data and behavior other than used for the UI.
The view respresents the display of the model in the UI. Thus, if our model is a customer object our view might be a frame full of UI widget or an HTML page rendered with information from the model. The view is only about display of information; any changes to the information are handled by the third member of the MVC trinity: the controller. The controller takes user input, manipulates the model, and causes the view to update appropriately. In this way UI is a combination of the view and the controller.
-- 引自 Patterns of Enterprise Application Architecture通过马丁福勒
你的问题
View 观察并响应模型的变化。型号是Domain Model而不是单个记录集或实体。
勘误表
当前 MVC 的常用方式与 Martin Fowler 创造的原始 MVC 模式不同。他基于此模式在 Smalltalk .
At the heart of MVC, and the idea that was the most influential to later frameworks, is what I call Separated Presentation.
MVC 的另一部分是模型、 View 和 Controller 如何交互。
In this case all the views and controllers observe the model. When the model changes, the views react.
这与 流行的 MVC 非常不同。 Ruby on Rails , Controller 负责准备和加载 View 。
class ArticlesController < ApplicationController
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
Martin Fowler 将 MVC 浓缩为这个
- Make a strong separation between presentation (view & controller) and domain (model) - Separated Presentation.
- Divide GUI widgets into a controller (for reacting to user stimulus) and view (for displaying the state of the model). Controller and view should (mostly) not communicate directly but through the model.
- Have views (and controllers) observe the model to allow multiple widgets to update without needed to communicate directly - Observer Synchronization.
-- 引自 GUI Architectures通过马丁福勒
关于model-view-controller - MVC 模式。 Model、View、Controller的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29005264/