Re: Proper way to create a singleton without @synchronized ?
Re: Proper way to create a singleton without @synchronized ?
- Subject: Re: Proper way to create a singleton without @synchronized ?
- From: WT <email@hidden>
- Date: Sun, 17 Apr 2011 15:42:56 -0300
On Apr 17, 2011, at 3:11 AM, Dave DeLong wrote:
> This is a truly strange question. If you interpret a singleton to be "one and only one", then it doesnt make much sense to ask about subclassing it, because that would imply that you could instantiate one of the base type and one of the sub type, thus making it not fit the definition of a singleton.
That's only because, unlike java, obj-c doesn't have a way built into the language to enforce a class to be abstract. I can see valid reasons to subclass a singleton if that singleton is to be used as an abstract class and never be instantiated by itself.
> However, if we put philosophical discussions aside and simply ask for a maximum of one instance per class, then I'd probably do something like this: instead of using a single static variable to hold your singleton, use a mutable dictionary instead. The keys of the dictionary would be the name of the class, and the corresponding values would be e singleton objects. Override +allocWithZone: to either return the appropriate and existing object, or capture the call to super and save it in the dictionary before returning it. Of course, this would preclude any of the subclasses doing [self release] and returning a new object, but that's probably an adequate compromise.
That's an interesting suggestion and it's something I'll consider it. Thanks for offering that suggestion.
WT
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden