Re: Is Apple's singleton sample code correct?
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