多个视图和控制器的 MVC 类组织是什么样的?
MVC 本身的想法对我来说似乎很清楚,但我很难理解这个概念如何“扩展到”多个视图和控制器。
看来 Cocoa 采用了将控制器作为模型和视图之间的“交换机”的方法。在多个视图的情况下,应用程序将如何组织和运行?如果有一个与每个视图关联的控制器,主应用程序是否必须跟踪它生成的所有控制器,或者每个控制器将具有它实例化的“嵌套”控制器,例如应用程序将创建一个窗口,该窗口将创建一个工具栏等?
如果整个应用程序需要使用相同的模型,或者您是否会将模型分解为更小的模型,该怎么办?
看来我真正要问的是如何将多窗口/视图应用程序拆分为其逻辑块,同时保留模块化 MVC 结构。我尝试查看 WordPress iPhone 应用程序和 Adium 中的代码,但它们似乎都有一个相对较大的代码库,让我迷失了方向。
The idea of MVC itself seems clear to me but I have trouble understanding how the concept 'scales up' to multiple views and controllers.
It appears that Cocoa takes the approach of the controller being the 'switchboard' between the model and the view. How would the application be organized and function in case of multiple views? If there is a controller associated with every view, would the main application have to keep track of all the controllers it spawns, or each controller would have 'nested' controllers it instantiates, e.g. an application would create a window, the window would create a toolbar etc?
What if the entire application would need to work with the same model, or would you break the models down into smaller ones?
It seems like what I'm really asking is how you would split a multi-window/view application into its logical blocks while retaining the modular MVC structure. I've attempted to look at code from the WordPress iPhone app as well as Adium but both seem to have a relatively large code base that I get lost in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,控制器是分层实现的。例如,在标准的 Cocoa Document 架构中,您有一个
NSDocumentController
来管理NSDocument
的多个实例。每个NSDocument
实例管理一个或多个NSWindowController
实例,每个NSWindowController
实例可以管理一个或多个NSViewController< 实例/代码>。
随着层次结构的向下移动,控制者的职责变得更加具体和细粒度。在访问模型方面,Cocoa 有多种模式,例如委托和数据源模式,您可以使用它们来允许视图进行绘制,而视图不需要了解有关模型本身的任何信息。
一般来说,应用程序将有一个统一的模型,除非有不同的模型有意义(例如,如果您需要应用程序编辑不同类型的文档)。
Generally controllers are implemented hierarchically. For example, in the standard Cocoa Document architecture, you have an
NSDocumentController
that manages multiple instances ofNSDocument
. Each instance ofNSDocument
manages one or more instances ofNSWindowController
, and each instance ofNSWindowController
can manage one or more instances ofNSViewController
.As you move down the hierarchy, the controllers become more specific and fine-grained in their responsibilities. In terms of accessing the model, Cocoa has several patterns such as the delegate and datasource patterns which you can use to allow the view to draw without the view needing to know anything about the model itself.
Generally the app would have a single unified model, unless it makes sense to have different models (for example, if you needed your app to edit different types of document).