问题描述
我刚刚开始研究 iPhone 应用.我怎么知道什么时候应该把东西放在 AppDelegate 中而不是自定义类中?是否有其他编程语言(如 Python 或 PHP)使用类似 AppDelegate 的模式的规则或任何类型的类比?
I'm just beginning to work on iPhone apps. How do I know when I should be putting stuff in AppDelegate versus a custom class? Is there a rule or any type of analogy with another programming language like Python or PHP that uses an AppDelegate like pattern?
推荐答案
我通常会避免使用 Andrew 使用的术语应用程序的核心"所暗示的设计方法.我的意思是,我认为您应该避免将太多东西集中在一个中心位置——好的程序设计通常涉及通过关注区域"来分离功能.
I normally avoid the design approach implied by Andrew's use of the term "heart of your application". What I mean by this is that I think you should avoid lumping too many things in a central location -- good program design normally involves separating functionality by "area of concern".
委托对象是当它所连接的对象达到特定事件或状态时得到通知的对象.在这种情况下,Application Delegate 是一个在 UIApplication 对象达到特定状态时接收通知的对象.在很多方面,它是一种专门的一对一观察者模式.
A delegate object is an object that gets notified when the object to which it is connected reaches certain events or states. In this case, the Application Delegate is an object which receives notifications when the UIApplication object reaches certain states. In many respects, it is a specialized one-to-one Observer pattern.
这意味着 AppDelegate 的关注区域"正在处理特殊的 UIApplication 状态.其中最重要的是:
This means that the "area of concern" for the AppDelegate is handling special UIApplication states. The most important of these are:
- applicationDidFinishLaunching:- 适合处理启动时的配置和构建
- applicationWillTerminate: - 适合最后清理
您应该避免将其他功能放在 AppDelegate 中,因为它们并不真正属于那里.此类其他功能包括:
You should avoid putting other functionality in the AppDelegate since they don't really belong there. Such other functionality includes:
- 文档数据 -- 您应该有一个文档管理器单例(用于多个文档应用程序)或一个文档单例(用于单文档应用程序)
- 按钮/表格/视图控制器、视图委托方法或其他视图处理(除了在 applicationDidFinishLaunching 中构建顶级视图:)——这项工作应该在各自的视图控制器类中.
许多人将这些东西混入他们的 AppDelegate 中,因为他们很懒惰,或者他们认为 AppDelegate 控制了整个程序.您应该避免集中在您的 AppDelegate 中,因为它会混淆应用中的关注区域并且无法扩展.
Many people lump these things into their AppDelegate because they are lazy or they think the AppDelegate controls the whole program. You should avoid centralizing in your AppDelegate since it muddies the areas of concern in the app and doesn't scale.
这篇关于AppDelegate 的用途是什么,我如何知道何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!