Singleton 访问器名称背后的原因是什么?
在几个项目的过程中,我编写了大量的类工厂方法来访问 单例 类的实例。我一直使用 + (id)sharedSomething;
命名约定的一些变体。
另一方面,苹果有多种命名约定。例如:
// NSNotificationCenter
+ (id)defaultCenter;
// NSUserDefaults
+ (NSUserDefaults *)standardUserDefaults;
// UIApplication
+ (UIApplication *)sharedApplication;
在命名我自己的方法时,我应该注意这些名称中名词前面的形容词是否有任何押韵或原因?我最初认为这可能与“灵活”与“严格”单例设计有关,但 NSFileManager 和 NSNotificationCenter 都遵循 + (id)defaultSomething 约定,但 NSFileManager 支持其他实例的分配,而 NSNotificationCenter 不支持。我很困惑。
编辑:我错误地认为 NSNotificationCenter 不支持新中心的实例化。只是这种情况并不常见,因此最初的假设并不一定无效。
Over the course of a few projects, I have written a decent amount of class factory methods for accessing a Singleton instance of a class. I have always used some variation on the + (id)sharedSomething;
naming convention.
Apple, on the other hand, has a variety of naming conventions. For example:
// NSNotificationCenter
+ (id)defaultCenter;
// NSUserDefaults
+ (NSUserDefaults *)standardUserDefaults;
// UIApplication
+ (UIApplication *)sharedApplication;
Is there any rhyme or reason to the adjective placed before the noun in those names that I should be aware of when naming my own methods? I originally thought it might have something to do with "flexible" vs "strict" singleton designs, but NSFileManager and NSNotificationCenter both follow the + (id)defaultSomething convention, yet NSFileManager supports the allocation of other instances while NSNotificationCenter does not. I'm stumped.
EDIT: I was wrong in thinking NSNotificationCenter does not support the instantiation of new centers. It's just not terribly common, so the original hypothesis is not necessarily invalidated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,
shared...
方法适用于真正的单例(NSApplication、NSWorkspace 等),而default...
或standard...
表示一个类可以有效地实例化,但大多数客户会很高兴只使用单个全局实例。但没有任何公开的书面标准,而且似乎主要是在编写此类课程时做出的决定。In general, the
shared…
methods are for true singletons (NSApplication, NSWorkspace, etc.) whiledefault…
orstandard…
denotes a class that can be usefully instantiated, but where most clients will be happy just working with a single global instance. But there isn't any public written standard, and it seems primarily to have been a decision made whenever such a class was written.我完全偶然地从 Craig Hockenberry 所著的 iPhone App Development 书中发现了关于该主题的这个立场:
我将引用它作为 @Chuck 发布的答案的替代方案。总结似乎是没有真正的模式,而 Chuck 指出了真正的单例和通常用作单例的对象之间的粗略相关性,而 Hockenberry 先生则指出了年龄上的粗略相关性。
I found, by complete happenstance, this position on the topic from the book iPhone App Development by Craig Hockenberry:
I'll cite it as an alternative to the answer posted by @Chuck. The summary seems to be that there is no real pattern, while Chuck points to a rough correlation between true singletons and objects that are just commonly used as singletons, and Mr. Hockenberry points to a rough correlation in age.
它与“灵活”与“严格”单例设计无关。 NSNotificationCenter 和 NSUserDefaults 甚至都不是单例。您真的认为“flexibleSingleNotificationCenter”这个名称比“defaultCenter”更好吗?是否有任何单一方法可以帮助该方法的用户了解该方法是如何实现的?
NSDeteriministicFiniteStateMachineToggleButton 有人吗?
It has nothing to do with "flexible" vs "strict" singleton designs. NSNotificationCenter and NSUserDefaults are not even Singletons. Do you really think flexibleSingleNotificationCenter is a better name than defaultCenter? Is there any single way that it helps the user of the method to know how it is implemented?
NSDeteriministicFiniteStateMachineToggleButton anyone?