什么应该在MVC模式中拥有模型? [英] What should own the model in an MVC pattern?

查看:138
本文介绍了什么应该在MVC模式中拥有模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我记得以来,我一直在制作iOS应用程序,但直到最近我实习编程时,我还没有成熟我的编程风格。我很早就学到了很多OO概念,因为我意识到生活对它们的理解很糟糕,但我从未学过的一件事就是MVC模式。

I have been making iOS apps since I can remember, but I haven't matured my programming style much until recently when I got an internship programming. I learned many OO concepts early on because I realized that life sucked with out an understanding of them, but one thing that I never made myself learn was the MVC pattern.

To给出上下文,假设我在一个 SolarSystemView UIView 的子类)中绘制太阳系。我的 SolarSystemView 是否有一个类的实例变量SolarSystem (一个包含所有重要行星和数据结构的数据结构的类) stelar属性),或者应该是 SolarSystemViewController 的实例的所有权?或者它是完全不同的东西?我找不到任何给出令人满意的答案的示例代码。

To give context, let's say that I'm drawing a solar system inside of a single SolarSystemView (a subclass of UIView). Should my SolarSystemView have an instance variable of class SolarSystem (a class containing a data structure with all of the important planetary and stelar properties), or should that be under ownership of an instance of a SolarSystemViewController? Or is it something completely different? I can't find any example code that gives a satisfactory answer.

我想如果视图拥有模型,操作会非常顺利,但也不会感觉好风格。毕竟, SolarSystem 实例必须以某种方式动态更改,并且具有与 SolarSystemView 更新相同或相似的速率。

I imagine that if the view owned the model, operations would be very smooth, but that also doesn't feel like good style. After all, the SolarSystem instance has to change dynamically somehow, and with the same or similar rate that the SolarSystemView updates.

推荐答案

在MVC范例中,模型应该与View分开。为了获得它需要绘制的数据,它向Controller询问它,然后它会向模型询问它。这样,我们就可以从GUI中分离出信息。将其视为模型控制器查看是否有帮助。因此,在大多数情况下,Controller拥有模型。

In the MVC paradigm, the Model is supposed to be separated from the View. To get the data it needs to draw itself, it asks the Controller for it, which in turn asks the Model for it. That way, we decouple the information from the GUI. Think of it as Model Controller View if it helps. Therefore, in most cases, the Controller "owns" the Model.

例如,在cs193p中, CalculatorViewController (Controller)有一个 CalculatorBrain (Model)属性,它与之交互以获取要在视图中显示的方程式结果。

For example, in cs193p, the CalculatorViewController (Controller) has a CalculatorBrain (Model) property, which it interacts with to get the results of equations to display in the View.

在您的特定示例中, SolarSystemViewController 可能会强烈引用 SolarSystem ,它是将轮询数据,它将传递 SolarSystemView ,以便它可以在需要更新时自行绘制。当用户与之交互时, SolarSystemView 也可能会通知 SolarSystemViewController ,以便它可以显示其他视图或更新 SolarSystem ,以及它可以执行的任何其他任务。

In your particular example, the SolarSystemViewController would probably have a strong reference to the SolarSystem, which it would poll for data that it would hand off the the SolarSystemView so that it could draw itself when it needed updating. The SolarSystemView might also notify the SolarSystemViewController when the user interacts with it so that it can display other views or update the SolarSystem, among any other tasks it could perform.

请注意,Cocoa和Cocoa Touch中的MVC范例有点不同于更常见的MVC版本,就像Smalltalk一样。例如,如果您查看MVC上的维基百科页面图表应该与您学习的内容不同。实际上,GoF(设计模式)因此描述了MVC。

Note that the MVC paradigm in Cocoa and Cocoa Touch is a little different than the more generalized version of MVC seen elsewere, like Smalltalk. For example, if you look at the Wikipedia page on MVC the diagram there should look different than what you've been learning. In fact, GoF (Design Patterns) describes MVC thusly.


MVC由三种对象组成。 Model是应用程序
对象,View是它的屏幕显示,Controller
定义用户界面对用户输入的反应方式。在MVC之前,
用户界面设计倾向于将这些对象混为一谈。 MVC
将它们分离以增加灵活性和重用。 MVC通过在它们之间建立订阅/通知协议来解耦视图
和模型。
视图必须确保其外观反映模型的状态。
每当模型的数据发生变化时,模型会通知视图
依赖于它。作为回应,每个视图都有机会更新
本身。此方法允许您将多个视图附加到模型,以
提供不同的演示文稿。您还可以为
模型创建新视图而无需重写。

MVC consists of three kinds of objects. The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input. Before MVC, user interface designs tended to lump these objects together. MVC decouples them to increase flexibility and reuse. MVC decouples views and models by establishing a subscribe/notify protocol between them. A view must ensure that its appearance reflects the state of the model. Whenever the model's data changes, the model notifies views that depend on it. In response, each view gets an opportunity to update itself. This approach lets you attach multiple views to a model to provide different presentations. You can also create new views for a model without rewriting it.

在这两种情况下,模型本身都在联系要更新它的视图。但是,在iOS上,Model和View之间的交互是通过Controller处理的。这在 cs193p第一次会议中有详细解释以及 Apple自己的关于MVC关系的文档。这样,您只需要重写Controller代码以重用其他地方的模型和视图。

In both these cases, the Model itself is contacting the View to update it. However, on iOS, interaction between the Model and the View are handled through the Controller. This is well explained in the first session of cs193p as well as Apple's own documentation on MVC relationships. That way, you only need to rewrite the Controller code to reuse the Models and Views elsewhere.

这是从cs193p到澄清的MVC图。

Here's the MVC diagram from cs193p to clarify.

这篇关于什么应该在MVC模式中拥有模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆