Re: Class clusters - infinite loop?
Re: Class clusters - infinite loop?
- Subject: Re: Class clusters - infinite loop?
- From: Ben Dougall <email@hidden>
- Date: Sat, 25 Jun 2005 12:11:09 +0100
On Saturday, June 25, 2005, at 07:08 am, Steve Canfield wrote:
This is a repost from what I just posted to
comp.sys.mac.programmer.help, but I thought cross posting might be
helpful.
I'm trying to create a class cluster.
First is my super class -- this is the init method
DataHolder.m
- (id)initWithData:(NSData *)someData {
// look at the data
if([someData length] > 100) {
self = [[BigDataHolder alloc] initWithData:someData];
}
else {
self = [[SmallDataHolder alloc] initWithData:someData];
}
return self;
}
IE we chose the subclass based on the data... seems simple enough
Now onto the subclass's init methods
BigDataHolder.m // basically same for SmallDataHolder
- (id)initWithData:(NSData *)someData {
if(self = [super initWithData:someData]) {
// do some initialization here, etc
}
return self;
}
Anyway, I think you can see the problem here. This is going to go into
an infinite loop. What's the solution? Should I skip the call to super
and just call super super's init? Should I class sniff in my
DataHolder init? Is there some simple way out of this?
Class clusters seem cool but the documentation on how to actually
implement them has been a little off so far.
i don't think you can add on the BigDataHolder part/extension of an
object instance to an already alloc/inited DataHolder instance, which
is what you're trying to do there i think.
there may be a number of different ways to implement class clusters,
but here's one: when the class cluster is first used alloc and init a
singleton instance of DataHolder, then in that singleton alloc and init
the appropriate subclass and return that (an instance of
Small/BigDataHolder), *not* 'self' (instance of DataHolder), from the
abstract class, so the DataHolder singleton instance is never returned
to the requesting object, only used (as a middle man as it were). then
the next time the class cluster is used repeat all that apart from the
"alloc and init a singleton instance of DataHolder" bit because
DataHolder will already exist.
so basically, in the above way to do it, there are two object instances
(not counting the object that's requesting the object from the class
cluster) involved and required in the initiation of a class cluster
object (but one of those is a singleton so it isn't quite as
inefficient as it first sounds).
ben.
_______________________________________________
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