多个视图和控制器的MVC类组织是什么样子? [英] What does MVC class organization look like for multiple views and controllers?

查看:204
本文介绍了多个视图和控制器的MVC类组织是什么样子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC本身的想法对我来说似乎很清楚,但是我很难理解这个概念如何扩大到多个视图和控制器。

The idea of MVC itself seems clear to me but I have trouble understanding how the concept 'scales up' to multiple views and controllers.

看来Cocoa需要控制器的方法是模型和视图之间的交换机。在多个视图的情况下,应用程序如何组织和运行?如果存在与每个视图相关联的控制器,则主应用程序必须跟踪它产生的所有控制器,或者每个控制器将具有嵌套控制器。应用程序将创建一个窗口,窗口将创建一个工具栏等?

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?

看起来像我真正要求的是如何将多窗口/视图应用程序拆分为其逻辑块,同时保留模块化的MVC结构。我试图看看WordPress iPhone应用程序和Adium的代码,但似乎都有一个相对较大的代码基础,我迷路了。

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.

推荐答案

一般来说,控制器是分层实现的。例如,在标准的Cocoa Document架构中,您有一个 NSDocumentController ,用于管理 NSDocument 的多个实例。 NSDocument 的每个实例管理 NSWindowController 的一个或多个实例,并且每个实例 NSWindowController 可以管理 NSViewController 的一个或多个实例。

Generally controllers are implemented hierarchically. For example, in the standard Cocoa Document architecture, you have an NSDocumentController that manages multiple instances of NSDocument. Each instance of NSDocument manages one or more instances of NSWindowController, and each instance of NSWindowController can manage one or more instances of NSViewController.

控制器变得更具体和细致的责任。在访问模型方面,Cocoa有几个模式,例如委托和数据源模式,你可以使用它来允许绘制视图,而不需要知道模型本身的任何信息。

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).

这篇关于多个视图和控制器的MVC类组织是什么样子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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