• 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: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: David Gimeno Gost <email@hidden>
  • Date: Sun, 27 Nov 2005 00:14:49 +0100

On 26 Nov 2005, at 18:43, Uli Kusterer wrote:

Weren't we trying to make our code safer and more readable? I hope I never end up writing code like

	[[[self alloc] init] release]

in any of my programs. This code effectively does nothing, and its side effects do everything. Yes, the comment elaborates on that, but in this case I'd prefer self-documenting code.

Actually, the problem with that code is not that it's not self-documenting, but that the two methods that are involved depend too much on each other. The ugly code is just a consequence of this (the +sharedInstance method shouldn't have to undo what +allocWitZone: did).


This strong coupling could be greatly reduced by the introduction of another static variable to flag whether +allocWithZone: does indeed need to increase the retain count.

But these are implementation details and I'm currently more interested in proving that the lifetime constraints should be removed from the design of the class and that, by doing that, one can design a singleton class that can satisfy any lifetime constraints while, at the same time, being better in every aspect (simpler, clearer, safer, more useful, more reusable, etc).

BTW, I like the solution you propose. I find interesting the idea of throwing away any instance that has not been created by calling the +sharedInstance method. I'll have to look at it more closely.

Regards.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Is Apple's singleton sample code correct?
      • From: mmalcolm crawford <email@hidden>
References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Shawn Erickson <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)

  • Prev by Date: Update NSMenu / NSMenuItem while they are displayed
  • Next by Date: Cocoa Recipes for Mac OS X
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread