Re: Class Clusters
Re: Class Clusters
- Subject: Re: Class Clusters
- From: John Anderson <email@hidden>
- Date: Tue, 20 May 2003 11:22:43 -0400
Dear Dr. Drew,
I strongly recommend that you study the ClassCluster tutorial written
by "KritTer" (Chris Purcell) at CocoaDev:
http://www.cocoadev.com/index.pl?ClassClusters
Basically, the "better way" of dealing with your problem of creating
an infiinite loop and such is by implementing a private "placeholder"
class that handles the initialization and allocation of your concrete
classes of your cluset like so:
DataSet
+ (id)alloc
{
if ([self isEqual:[MyMatrix class]])
return [MyPlaceholderMatrix alloc];
else
return [super alloc];
}
-init {returns nil }
-initWithData {returns nil }
-initWithFileHandle {returns nil }
PlaceholderDataSet
-init {returns [[InMemoryDataSet alloc] init ...}
-initWithData {returns [[InMemoryDataSet alloc] init ...}
-initWithFileHandle {returns [[FileHandleDataSet alloc] init ...}
Truly yours,
John Anderson
On Tuesday, May 20, 2003, at 06:56 America/Detroit, Drew McCormack
wrote:
>
I am trying to write a simple class cluster, but have come up against
>
a problem. My basic design is this:
>
>
DataSet
>
-init (returns a InMemoryDataSet)
>
-initWithData (returns InMemoryDataSet)
>
-initWithFileHandle (returns FileHandleDataSet)
>
>
FileHandleDataSet : DataSet
>
-initWithFileHandle
>
>
InMemoryDataSet : DataSet
>
-initWithData
>
>
DataSet is the public abstract base class to the other two.
>
If the initWithData method of DataSet gets called, it tries to create
>
an InMemoryDataSet object and return that in its place. To do this it
>
calls the "initWithData" method of InMemoryDataSet. But the
>
initWithData method of InMemoryDataSet needs to initialize its
>
superclass by calling init, ie,
>
>
-(id)initWithData:(NSData *)data {
>
self = [super init];
>
return self;
>
}
>
>
This calls the init method of DataSet, which is also designed to
>
create and return a InMemoryDataSet, creating an infinite loop.
>
How should this be avoided? Should I just check the class of the
>
sender in the init method of DataSet to determine my course of action,
>
or is there a better way?
>
>
ie.
>
>
-(id)init {
>
if ( [[self class] isEqual:[DataSet class]] ) {
>
[self release];
>
self = [[InMemoryDataSet alloc] initWithData:nil];
>
}
>
else {
>
self = [super init];
>
}
>
return self;
>
}
>
>
Drew
>
>
========================================
>
Dr. Drew McCormack (Kmr. R153)
>
Afd. Theoretische Chemie
>
Faculteit Exacte Wetenschappen
>
Vrije Universiteit Amsterdam
>
De Boelelaan 1083
>
1081 HV Amsterdam
>
The Netherlands
>
>
Email email@hidden
>
Telephone +31 20 44 47623
>
Mobile +31 6 483 21307
>
Fax +31 20 44 47629
>
_______________________________________________
>
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.
_______________________________________________
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.