需要 MVC 模式的帮助
我刚刚开始阅读有关 MVC 模式的内容,但我并不完全理解它。
模型管理行为并 应用领域的数据, 回应信息请求 关于其状态(通常来自 查看),并响应指令 改变状态(通常从 控制器)。在事件驱动系统中, 该模型通知观察者(通常 视图)当信息发生变化时 他们可以做出反应。
视图将模型渲染成表单 适合交互,通常是 用户界面元素。多种视图 可以存在于单个模型中 不同的目的。视口 通常是一对一的 与显示表面的对应 并知道如何渲染它。
控制器接收用户输入并 通过拨打电话发起响应 在模型对象上。控制器接受 来自用户的输入并指示 用于执行操作的模型和视口 基于该输入。
(来自 http://en.wikipedia.org/wiki /Model%E2%80%93view%E2%80%93controller)
和
模型。模型管理行为 和应用领域的数据, 回应信息请求 关于其状态(通常来自 查看),并响应指令 改变状态(通常从 控制器)。
查看。视图管理显示 信息。
控制器。控制器解释 鼠标和键盘输入 用户,通知模型和/或 查看适当的更改。
(来自 http://msdn.microsoft.com/en-us/library/ff649643 .aspx)
我想我理解模型和视图是什么,但是我不理解控制器。从我读到的文本和一些示例中我看到控制器接受用户的输入。我见过这样的例子(在Java中):控制器是一个JOptionPane,它从用户那里接收一个华氏度的数字。然后在模型中对其进行分配和转换,并在命令行中显示结果。我不明白为什么控制器会接受这样的输入?
在 Ruby on Rails 中,我有存储数据的模型、显示不同视图并对视图中接收的输入执行“操作”的控制器。
I have just started to read about the MVC pattern and I do not understand it completely.
The model manages the behavior and
data of the application domain,
responds to requests for information
about its state (usually from the
view), and responds to instructions to
change state (usually from the
controller). In event-driven systems,
the model notifies observers (usually
views) when the information changes so
that they can react.The view renders the model into a form
suitable for interaction, typically a
user interface element. Multiple views
can exist for a single model for
different purposes. A viewport
typically has a one to one
correspondence with a display surface
and knows how to render to it.The controller receives user input and
initiates a response by making calls
on model objects. A controller accepts
input from the user and instructs the
model and viewport to perform actions
based on that input.
(from http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)
And
Model. The model manages the behavior
and data of the application domain,
responds to requests for information
about its state (usually from the
view), and responds to instructions to
change state (usually from the
controller).View. The view manages the display of
information.Controller. The controller interprets
the mouse and keyboard inputs from the
user, informing the model and/or the
view to change as appropriate.
(from http://msdn.microsoft.com/en-us/library/ff649643.aspx)
I think I understand what the Model and View are however I do not understand the controller. From the text that I read and some examples I have seen the controller takes input from the user. I have seen examples such as (in Java): The controller is a JOptionPane that receives a number in fahrenheit from the user. Then assign and convert it in the model and shows the result in the command line. I do not understand why the controller takes input like that?
In Ruby on Rails I have the model that stores the data, the controller that displays different views and do "stuff" on the input that was received in the View.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之,
视图 - 负责从模型中绘制或呈现信息
模型 - 负责您的业务逻辑,并存储应用程序的状态
控制器 - 负责解释用户的操作并调用必要的事件模型并通知视图自行更新(如果需要)。
视图应该只负责绘制/渲染,因此它不应该知道用户交互时应该发生什么事件。模型应该只对业务逻辑和数据感兴趣。它不应该知道有关视图的任何信息。因此,控制器将这两部分联系在一起,允许模型可重用,并在单个模型上启用多个视图(想想同一数据模型的移动/电视/Web 视图)。
在 Web 世界中,控制器是接收 HTTP 输入的部分,视图是 HTML/JSP 代码,控制器是 Servlet(或等效框架),它作用于 HTTP 请求以在模型中执行必要的业务。
Put simply,
The View - is responsible for drawing or rendering the information from the Model
The Model - is responsible for your business logic, and storing the state of your application
The Controller - is reponsible for interpreting the user's actions and invoking the necessary events on the Model and informing the View to update itself (if required).
The View should only be responsible for drawing/rendering, so it should not know about what events should occur on a user's interaction. The Model should only be interested in business logic and data. It should not know anything about the View. The controller therefore ties these two pieces together, allowing for the Model to be re-usable and to enable Multiple Views over a single Model (think mobile / TV / Web view of the same data model).
In a Web world, your Controller is the piece that receives your HTTP inputs, your View is your HTML/JSP code and your Controller is the Servlet (or framework equivalent) that acts on the HTTP request to perform the necessary business in the model.
正如名称控制器所示,控制器解释输入并协调/控制模型和视图之间的通信,即它会首先触发模型更新,然后(如果视图不直接观察)模型)将模型更改通知视图,以便它们可以自行更新。
As the name controller indicates already, the controller interprets the input and coordinates/controls communication between the model and the views, i.e. it would first trigger the model update and then (if the views don't directly observe the model) notify the views of the model change so that they can update themselves.
控制器基本上接受用户交互,并使用某种映射,通过调用模型来发起响应。该模型将具有数据操作的业务逻辑。一旦控制器收到模型的响应,控制器会将模型传递给视图,视图将渲染一个用于查看和用户交互的界面(基于模型)。
模型很好,控制器必须负责用户交互,视图则负责将模型渲染到 UI。
The controller basically takes a user interaction and, using some kind of mapping, it will initiates a response by calling the model. The model will have the business logic for data manipulation. Once the controller has the response from the model, the controller will pass the model to the view will render an interface for viewing and user interaction (based on the model).
Model is fine, the controller must be responsible for user interaction and the view for the rendering of the model to a UI.
想象一个非常基本的应用程序:一个反映数据库表的模型(Java bean)、一个带有标签和文本字段的对话框(用于显示所有值)以及一个用于刷新内容的魔术按钮。
控制器现在具有在用户单击(也称为按下)按钮小部件后用新值更新文本字段的逻辑。
在这个简单的情况下:
文本字段中的用户输入 - 如果您有数据绑定,那么数据绑定本身就是控制器的责任。因为数据绑定向模型和视图发送更新信号。如果您的应用程序不使用数据绑定,则模型更新由操作(按钮单击)或某些事件(焦点更改等)触发。这个逻辑又是控制器的一部分。
顺便说一句:视图和控制器实现了经典的策略模式:vew 是一个配置了策略的对象(Head First 设计模式,第 532 页)
Imagine a very basic application: one model (a Java bean) that reflects a database table, a dialog with labels and textfields to display all values and a magic button to refresh the content.
The controller now has the logic to update the textfields with new values after the user has clicked on (aka pressed the) the button widget.
In this simple case:
User input in textfields - if you have databinding, then the databinding itself is a controller's responsibility. Because databinding sends update signals to model and view. If your application does not use databinding, then the model update triggered by an action (button click) or some event (focus change, ...). And this logic is part of the controller again.
BTW: The view and controller implement the classic Strategy Pattern: the vew is an object that is configured with a strategy (Head First Design Patterns, p. 532)
输入字段(例如JTextField)是视图的一部分,确定对哪些交互事件(键入字符、失去焦点、单击按钮...)执行什么操作的逻辑位于控制器中。
就像在您的示例中一样,控制器的逻辑可以与视图位于同一类中。如果您扩展 JOptionPane 并实现 ActionListener,以便 JOptionPane 可以对 JTextField 的事件做出反应,就会出现这种情况。 但是如果您想建立清晰的 MVC 模式,这显然是不鼓励的,因为您会混合视图和控制器的代码。
The input field (e.g. JTextField) is part of the View, the logic determining what to do on what interaction events (typing a character, loosing focus, clicking a button ...) is in the Controller.
Like in your example, the logic of the Controller COULD be in the same class as the View. This would be the case if you extend JOptionPane and also implement the ActionListener so that the JOptionPane can react to events of the JTextField. BUT this is clearly discouraged if you want to establish a clear MVC pattern, as you would mix code of the View and the Controller.