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: Jeff Laing <email@hidden>
- Date: Fri, 2 Dec 2005 15:41:12 +1100
>
This is no different than making a "copy" of an immutable
>
object such as a NSData or NSString... most classes will simply retain the
>
object, which isn't allowed to change or move.
Agreed. I wouldn't copy those either. Why would you?
Please remember, I'm asking this from the novices perspective? I seriously
don't know why I would call [NSString copy] since NSString is supposed to be
immutable.
Alright, I do know that it might really be an NSMutableString under the
covers so its a poor example.
>
Here is a very good example of where a mortal singleton is the best
>
choice: DSP. For instance, to perform FFT, you need to create a
>
complex array of "twiddle factors" which are the cos/sin
>
coefficients, and it's a large object and expensive to create, so as
>
long as any DSP objects are using the shared fft_setup :
>
MortalSingleton object, great, but as soon as none of them need it,
>
it gets deallocated.
Its probably arguable as to whether your use of the singleton pattern for
the above matrix of coefficients is appropriate. I think Malcolm has
already pointed out other people confusing "singleton" with "shareable,
cached object".
Nevertheless, I wasn't saying that there isn't a case for MortalSingleton -
its a worthwhile contribution to the Cocoa world.
What I was saying was that *I* dont understand why you'd *copy* a real
singleton, ie one of those objects of the class for which there should only
ever be one instance.
And because I can't concieve of a scenario where that would be rational, I'd
be inclined to prevent it from happening, rather than silently allow
misguided attempts from thinking they'd succeeded. Which is what the Apple
sample code does with -release.
Imagine suggesting, for completeness, the following:
@interface NSSingleton { ... }
+(NSSingleton*)singletonWithSingleton:(NSSingleton*)orig;
@end
@implementation NSSingleton
+(NSSingleton*)singletonWithSingleton:(NSSingleton*)orig { return orig; }
@end
(Hmmm, or should it return [NSSingleton +sharedInstance] ;-)
_______________________________________________
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