Re: is this enough for a singleton? (for a class cluster "placeholder")
Re: is this enough for a singleton? (for a class cluster "placeholder")
- Subject: Re: is this enough for a singleton? (for a class cluster "placeholder")
- From: Clark Cox <email@hidden>
- Date: Thu, 12 Feb 2004 09:39:15 -0500
On 2004/02/12, at 8:47, Ben Dougall wrote:
>
On Wednesday, February 11, 2004, at 09:56 pm, Shawn Erickson wrote:
>
>
>
>
> On Feb 11, 2004, at 1:22 PM, Ben Dougall wrote:
>
>
>
>> i got this code from the archives for a singleton object:
>
>>
>
>> + (id)allocWithZone:(NSZone *)zone {
>
>> if ([MyAbstractClass self] == self)
>
>> return NSAllocateObject([MyConcreteClass self], 0, zone);
>
>> return [super allocWithZone:zone];
>
>> }
>
>>
>
>> don't quite understand it -- will that give an ok/perfectly fine
>
>> singleton implementation? usually the singleton implementations that
>
>> i've seen involve a static variable in the class, but i know there's
>
>> a variety of ways to do singletons. is there something else that
>
>> needs to be done further to the above code, or is that it, so far as
>
>> the singleton aspect of it goes?
>
>
>
> The above looks more about returning a concrete sub-class in place of
>
> an abstract class, not a singleton.
>
>
i really do think it is for a singleton. that's clearly stated here
>
<http://cocoa.mamasam.com/COCOADEV/2003/05/2/64061.php> where
>
someone's talking about that exact piece of code and the
>
singleton/placeholder aspect of it.
>
>
>
> This is what I do for a singleton (not all is needed in theory but)...
>
>
[code snipped]
>
>
yup thanks for that code -- if i can't understand the above code i
>
will use another singleton implementation probably like yours, but i
>
would like to get to the bottom of the above bit of code first. it's
>
so short and simple for a start -- that's what makes me think that
>
something else maybe required that hasn't been said in the related
>
posts about it. anyone know if the above code does enough required for
>
a reasonable singleton implementation (for the abstract class of a
>
class cluster in case that makes a difference to the situation)?
There's nothing about the code posted that says "singleton" to me, in
fact, it prevents any instances of MyAbstractClass (except for
subclasses) from ever being created. I'll try walk you through the
method:
"if ([MyAbstractClass self] == self)"
This will be true if someone is trying to directly allocate an
instance of MyAbstractClass (i.e. not a subclass).
"return NSAllocateObject([MyConcreteClass self], 0, zone);"
This line will then allocate an instance of MyConcreteClass. The
result will be that any attempt to directly allocate MyAbstractClass
will get an instance of MyConcreteClass instead.
"return [super allocWithZone:zone];"
If, on the other hand, someone is trying to allocate a subclass, let
the allocation proceed normally
--
Clark S. Cox III
email@hidden
http://homepage.mac.com/clarkcox3/
http://homepage.mac.com/clarkcox3/blog/B1196589870/index.html
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.