MVC在基于Cocoa文档的应用程序中 [英] MVC in a Cocoa document-based application

查看:180
本文介绍了MVC在基于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.

我有几个关键类使用我的应用程序:NSPersistentDocument,NSWindowController和一个模型类。

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

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

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

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

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.

喜欢将功能从我现有的两个控制器移动到新的控制器类,它将作为模型控制器和视图控制器之间的控制器。最后,这仍然只是MVC设计模式,只有一些更多的结构。

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.

但是,我有困难,弄清楚这将如何适应Cocoa的文档 - 基于应用程序架构。

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

我最大的问题是在哪里和如何创建这个新的控制器对象?
这如何适应Cocoa的架构?

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?

谢谢。

推荐答案

拥有模型控制器和视图控制器的本能。这是一个非常好的心理分类,如何M和V通常挂在一起。但你仍然可以在MVC中使用纯粹的C,将整个操作绑定在一起。

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.

如果你正在谈论一个控制器, app:
把(big-C)控制器看成是你的应用程序的main()函数,在旧的Cocoa教程中,这个对象通常叫做AppController。它可能是UIApplication的委托,或者不是,但如果不是,您应该考虑在项目中的应用程序委托的 applicationDidFinishLaunching:方法中创建这样的主控制器。然后,AppController可以设置(和拥有)模型对象并设置(和拥有)您的UI弹出的根视图控制器。

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.

谈论一些中介组件有一个文档架构中的每个模型/视图对的多个实例,然后只是做这样的东西。 DocumentController 是你想要的那种名字,虽然Cocoa有一个已经可能反映你所需要的功能。 DocumentManager是另一个候选名称。

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.

这篇关于MVC在基于Cocoa文档的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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