Re: Class Clusters
Re: Class Clusters
- Subject: Re: Class Clusters
- From: John Anderson <email@hidden>
- Date: Sat, 24 May 2003 06:18:18 -0400
I found this post (listed below) by Apple's Chris Kane, in the
archives. From this I gathered that the real purpose of the
Placeholder class in class cluster architecture is to achive better
performance through the use of it as "singleton" object. More clearly,
instead of allocating and releasing an instance of your abstract class
(or placeholder) every time you create new instance from your cluster,
you instead simply use a single instance of Placeholder for this
purpose (that was already allocated in +initialize and not released).
That way when you create a new object via your cluster you minimize the
extra overhead of the class cluster shell game. Specifically,
[NSObject alloc] is only called once (instead of twice).
FROM: Chris Kane
DATE: 2001-09-16 23:50
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 REMOVED>> talked about this
in another email.
Chris Kane
Cocoa Frameworks, Apple
On Wednesday, May 21, 2003, at 23:16 America/Detroit, Nathan Day wrote:
>
When I do this sort of thing I just have some factor methods that
>
return autoreleased instances of the correct sub class and then
>
describe in the documentation that this is the correct way to get
>
instances and not init and alloc. Going to all of the effort of doing
>
the init/alloc mojo is overkill most of the time unless you are
>
creating some frame that will be made public.
>
>
On Wednesday, May 21, 2003, at 01:44 AM, Drew McCormack wrote:
>
>
> I actually did read this article. I don't really understand what the
>
> purpose of the placeholder class is. The article just says we do it
>
> "...because Apple does it...". I can't see the difference between
>
> returning a DataSet instance or a PlaceholderDataSet instance from
>
> DataSet's alloc method.
>
>
>
> And as far as I can tell, it doesn't actually help with my problem. My
>
> question is, in the init... methods of the private subclasses of
>
> DataSet, which DataSet constructor should I chain to? In the example
>
> you have given, the problem still exists, because your DataSet "init"
>
> method returns nil. So you can't do this in FileHandleDataSet, for
>
> example,
>
>
>
> -(id)initWithFileHandle:(NSFileHandle *)fh {
>
> if ( self = [super init] ) {
>
> ....
>
> }
>
> return self
>
> }
>
>
>
> because [super init] is the DataSet init method, which returns nil. I
>
> guess the init method of DataSet should be omitted, and then
>
> everything
>
> will work, right?
>
>
>
Nathan Day
>
http://homepage.mac.com/nathan_day/
_______________________________________________
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.