Re: Mutable classes and class clusters
Re: Mutable classes and class clusters
- Subject: Re: Mutable classes and class clusters
- From: Chris Kane <email@hidden>
- Date: Sun, 16 Sep 2001 14:50:18 -0700
On Saturday, September 15, 2001, at 12:48 PM, Rob Rix wrote:
2) One of the classes I'm implementing in my framework is about to be
redesigned as a class cluster, with a single public class (unless I
find that separating it into mutable and immutable would be worthwhile,
of course). So, to make a class cluster, is all that is involved
changing my class' -(id)init method so that it returns an instance of a
private subclass?
Or instead of allocating an object only to have it deallocated in -init,
override +allocWithZone: to allocate an instance of the appropriate
concrete class.
+ (id)allocWithZone:(NSZone *)zone {
if ([MyAbstractClass self] == self)
return NSAllocateObject([MyConcreteClass self], 0, zone);
return [super allocWithZone:zone];
}
Then the -init method will go right to an instance of the concrete
class. This works well when for any particular abstract class you know
which concrete class to use (ie, there is a one-to-one correspondence).
If you don't know which class you'll need until the init method is
called, or otherwise don't have enough information at +allocWithZone:
time to allocate the real object, then going through a placeholder
singleton object (returning that from the +allocWithZone:) is an
efficient way to go. Peter Ammon <email@hidden> talked about this
in another email.
Chris Kane
Cocoa Frameworks, Apple