Cocoa 基于文档的应用程序中的 MVC

发布于 2024-08-22 07:37:51 字数 611 浏览 6 评论 0原文

我目前正在对我的应用程序进行重构和重组。我意识到模型和视图及其控制器之间的一些分离已经减少,我希望进行一些清理。

我的应用程序中使用了几个关键类:NSPersistentDocument、NSWindowController 和模型类。

NSPersistentDocument 类充当“模型控制器”;它拥有模型类的一个实例,并管理与模型的所有交互。

NSWindowController 类充当“视图控制器”;它拥有主窗口,并管理主窗口内视图的交互。此类也是定义窗口的 nib 文件的文件所有者。

我在这里看到的问题是我没有真正的“控制器”。我当前的设计迫使模型控制器和视图控制器相互了解。两者之间没有冥想对象,因此,我的模型和视图没有明确分离,这使得支持多个视图或模型成为一个问题。

我想将功能从现有的两个控制器转移到一个新的“控制器”类中,该类将充当模型控制器和视图控制器之间的控制器。归根结底,这仍然只是MVC设计模式,只是结构多了一点。

然而,我很难弄清楚这如何适合 Cocoa 基于文档的应用程序架构。

我最大的问题是在哪里以及如何创建这个新的控制器对象? 这如何适应 Cocoa 的架构? 我是否在对抗 Cocoa 的架构,有没有更好的方法来做到这一点?

谢谢。

I'm going through a refactoring and reorganization of my application at the moment. I've realized that some of the separation between models and views, and their controllers has diminished and I wish to do some cleaning up.

I have several key classes used in my app: NSPersistentDocument, NSWindowController, and a model class.

The NSPersistentDocument class acts as a "model-controller"; it owns an instance of the model class, and manages all interactions with the model.

The NSWindowController class acts as a "view-controller"; it owns the main window, and manages interactions of the views within the main window. This class is also the File's Owner for the nib file in which the Window is defined.

The problem I see here is that I don't have a real "controller". My current design forces the model-controller and view-controller to know about each other. There is no meditating object between the two, and due to this, my model and view are not clearly separated, which makes supporting multiple views or models a problem.

I would like to move functionality from both of my existing controllers into a new "controller" class which would act as a controller between the model-controller and view-controller. In the end, this is still just the MVC design pattern, with just a little more structure.

However, I am having difficulty figuring out how this would fit into Cocoa's document-based app architecture.

The biggest question I have is where and how would this new controller object be created?
How does this fit into Cocoa's architecture?
Am I fighting against Cocoa's architecture, and is there a better way to do this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-08-29 07:37:51

对拥有“模型控制器”和“视图控制器”的强烈直觉。这是一个很好的心理分类法,可以解释 M 和 V 通常如何联系在一起。但正如您所注意到的,您仍然可以在 MVC 中使用纯“C”将整个操作联系在一起。

如果您正在谈论一个控制器,则对于应用程序:
将(big-C)控制器视为从应用程序的 main() 函数中衍生出来的东西——在较早的 Cocoa 教程中,该对象通常称为 AppController。它可能是 UIApplication 的委托,也可能不是,但如果不是,您应该考虑在项目中应用程序委托的 applicationDidFinishLaunching: 方法中创建这样的主控制器。然后,该 AppController 可以设置(并拥有)模型对象,并设置(并拥有)根视图控制器,您的 UI 将从中产生。

如果您正在谈论某个有多个实例的中介组件,文档架构中的每个模型/视图“对”都有一个实例,那么也可以制作类似的东西。 DocumentController 是您想要的名称,尽管 Cocoa 已经有一个可能反映也可能不反映您需要的功能类型的名称。 “DocumentManager”是另一个候选名称。

Great instincts on having a "model controller" and a "view controller". That's a very good mental taxonomy for how the M's and the V's usually hang together. But you can still have the pure "C" in the MVC to tie the whole operation together, as you note.

If you're talking about one controller, for the app:
Think of the (big-C) Controller as the thing that grows out of your app's main() function-- in older Cocoa tutorials this object is often called AppController. It might be the delegate of UIApplication, or not, but if it's not, you should consider creating such a master controller in the applicationDidFinishLaunching: method of the app delegate in your project. That AppController can then set up (and own) the model objects and set up (and own) the root view controller, from which your UI springs.

If you're talking about some mediating component that there are multiple instances of, one for each model/view "pair" in a document architecture, then just make something like that up also. DocumentController is the kind of name you want, although Cocoa has one of those already that may or may not reflect the kind of functionality you need. "DocumentManager" is another candidate name.

梦晓ヶ微光ヅ倾城 2024-08-29 07:37:51

听起来你需要拿起一份 Cocoa 设计模式,回答这些问题,然后是一些。

第 2 章讨论使用 ArrayController 作为模型控制器(而不是您正在使用的持久文档模型控制器)的 MVC 模式。

Sounds like you need to pick up a copy of Cocoa Design Patterns, answers these questions and then some.

Chapter 2 deals with the MVC pattern using an ArrayController as the model controller (rather than the persistent document model-controller you are using).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文