Re: Class clusters - infinite loop?
Re: Class clusters - infinite loop?
- Subject: Re: Class clusters - infinite loop?
- From: Ondra Cada <email@hidden>
- Date: Sat, 25 Jun 2005 13:38:31 +0200
Steve,
On 25.6.2005, at 8:08, Steve Canfield wrote:
I'm trying to create a class cluster.
First is my super class -- this is the init method
DataHolder.m
- (id)initWithData:(NSData *)someData {
Unless you overrode alloc (see below), you should begin with [self
release], lest the superclass instance leaks.
Also, some would argue that before [self release], "self=[super
init]" would be in order. Technically, it's true.
// look at the data
if([someData length] > 100) {
self = [[BigDataHolder alloc] initWithData:someData];
}
else {
self = [[SmallDataHolder alloc] initWithData:someData];
}
return self;
}
Yup, that's the gist of it.
There are a few tricks though to make the thing more efficient in
practical use. Far as I know, the commonest one is to re-implement
alloc so that it for the cluster (not a subclass) returns a singleton
(yeah, this is one of those pretty rare cases when you override alloc).
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?
That's the idea, though the concrete implementation would be easier:
you make a special designated initializer for subclasses at the
cluster level. That would be most probably init; since the cluster
does not keep data, there is no point in sending its initiWithData:
message anyway :)
Should I class sniff in my DataHolder init? Is there some simple
way out of this?
[written into mail directly, might not work as is, just the idea]
DataHolder.m:
+alloc {
// you may want to begin with "if (self==[DataHolder class])", see
also below
static DataHolder *singleton=nil;
if (!singleton) singleton=[[super alloc] init]; // yeah, moves
"init" into "alloc" (for the singleton only). Far as I know, no harm.
return singleton;
}
-init { // designated, to be used from subclasses' inits
if (!(self=[super init])) return;
...
return self;
}
-initWithData:(NSData*)d { // API
// if you wanted just use plain alloc here, you would have to
check in the reimplementation above OR override alloc in all subclasses
if ([d length]>100) return [NSAllocateObject([BigDataHolder class],
0,[self zone]) initWithData:d];
...
}
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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