开发者

Do singletons have any sense in Objective-c context?

I co开发者_Python百科me from java/cpp environment where singletons do have their advantages over class(static) methods, however in objective-c because of it's dynamic nature, from my past observation there is really no advantage in using a singleton over a class method. Could someone confirm this, or maybe give examples where a singleton can do something that a class method can't(but excepting the case in which a singleton is in fact a 'multiton")

EDIT: I know what singletons and class methods are and it's not a matter of concept, more like: can a class method fully replace a singleton in objective-c, rendering the singleton implementation useless?


Singletons are used in Objective-C. In the Cocoa framework, NSApplication, NSDocumentController, and NSWorkspace are examples of singletons.

The advantage of using a singleton over a class method is that the behavior of a singleton can be changed by making a subclass. It is somewhat rare, but some applications do make subclasses of NSDocumentController or NSApplication. (For example, you may wish to override the NSDocument -documentClassForType: method).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜