• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Proper way to create a singleton without @synchronized ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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:43:07 -0300

> it's possible the base class could be an abstract class, perhaps providing nothing except the ability for concrete subclasses to make singleton instances of themselves

Yes, that's precisely the use I have in mind, because it's the most common in my projects.

WT

On Apr 17, 2011, at 4:02 AM, Andy Lee wrote:

> I'm not really following this thread, so I might be missing important context, but I think it's possible the base class could be an abstract class, perhaps providing nothing except the ability for concrete subclasses to make singleton instances of themselves. You might reuse this class in different applications that need singletons. Or you might have multiple singleton classes within one application, all inheriting from the base class. Or within your app you might use one concrete class as a temporary placeholder for debugging, which would be replaced by the real class in production.
>
> --Andy
>
> On Apr 17, 2011, at 2: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.
>>
>> 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.
>>
>> Dave
>>
>> Sent from my iPad
>>
>> On Apr 16, 2011, at 10:20 PM, WT <email@hidden> wrote:
>>
>>> how would you implement a *true* singleton class that supports subclassing?

_______________________________________________

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

References: 
 >Proper way to create a singleton without @synchronized ? (From: WT <email@hidden>)
 >Re: Proper way to create a singleton without @synchronized ? (From: Kyle Sluder <email@hidden>)
 >Re: Proper way to create a singleton without @synchronized ? (From: WT <email@hidden>)
 >Re: Proper way to create a singleton without @synchronized ? (From: Dave DeLong <email@hidden>)
 >Re: Proper way to create a singleton without @synchronized ? (From: Andy Lee <email@hidden>)

  • Prev by Date: Re: Proper way to create a singleton without @synchronized ?
  • Next by Date: Re: Proper way to create a singleton without @synchronized ?
  • Previous by thread: Re: Proper way to create a singleton without @synchronized ?
  • Next by thread: Re: Proper way to create a singleton without @synchronized ?
  • Index(es):
    • Date
    • Thread