Singleton 访问器名称背后的原因是什么?

发布于 2024-10-18 01:37:22 字数 795 浏览 0 评论 0原文

在几个项目的过程中,我编写了大量的类工厂方法来访问 单例 类的实例。我一直使用 + (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 技术交流群。

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

发布评论

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

评论(3

空袭的梦i 2024-10-25 01:37:22

一般来说,shared... 方法适用于真正的单例(NSApplication、NSWorkspace 等),而 default...standard... 表示一个类可以有效地实例化,但大多数客户会很高兴只使用单个全局实例。但没有任何公开的书面标准,而且似乎主要是在编写此类课程时做出的决定。

In general, the shared… methods are for true singletons (NSApplication, NSWorkspace, etc.) while default… or standard… 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.

深居我梦 2024-10-25 01:37:22

我完全偶然地从 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:

Unfortunately, there's no one standard
for naming singletons. Older classes,
such as those in the foundation, use
the default prefix on the method
name. Newer classes tend to use
shared as a prefix.

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.

暖心男生 2024-10-25 01:37:22

它与“灵活”与“严格”单例设计无关。 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?

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