• 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: Greg Parker <email@hidden>
  • Date: Sun, 17 Apr 2011 22:52:09 -0700

On Apr 17, 2011, at 11:42 AM, WT wrote:

> Hi Ken,
>
> On Apr 17, 2011, at 3:00 AM, Ken Thomases wrote:
>
>> On Apr 17, 2011, at 12:20 AM, WT wrote:
>>
>>> On Apr 17, 2011, at 12:31 AM, Kyle Sluder wrote:
>>>
>>>>>     [[[[self class] alloc] init] autorelease];
>>>>
>>>> You are in a class method. self == [self class], so no need to call +class here.
>>>
>>> What if I'm trying to subclass this singleton?
>>
>> What about it?  'self' identifies the actual, dynamic object receiving the message -- in this case, a class.  It is not of the static type of the @implementation block it occurs within.  Put another way, [self class] is no more dynamic than [self alloc].
>
> Tell that to Erik Buck and Donald Yacktman, the authors of 'Cocoa Design Patterns'. They explicitly assert on the second paragraph of page 151 that [self class] is used instead of self to support instantiating the appropriate subclass. Clearly there is some confusion regarding this matter, even among people more experienced than I, so I don't feel so bad by being confused about it too. :)

Re-read that paragraph of Cocoa Design Patterns. It differs from what you said above:
"Of special note is the call to +alloc. Instead of sending it to MYGameHighScoreManager directly, we instead send it to [self class]."

[[self class] alloc] is more subclass-friendly than [MyClass alloc]. That's what Cocoa Design Patterns is trying to say.

[[self class] alloc] is no better than [self alloc], when inside a class method implementation. That's what Ken is trying to say.


--
Greg Parker     email@hidden     Runtime Wrangler


_______________________________________________

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: Ken Thomases <email@hidden>)
 >Re: Proper way to create a singleton without @synchronized ? (From: WT <email@hidden>)

  • Prev by Date: Re: Access main window of other Applications
  • Next by Date: Re: renaming directories and moving files into them (NSFileManager)
  • 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