Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a so called "Class Cluster" in Objective-C?

I was reading that NSArray is just such a thing. Sounds heavy. I have 7 really fat books here on my desk about Objective-C, Cocoa and C. None of them mention Class Cluster at all, at least I can't find it in the Index at the back of the books. So what's that?

like image 275
openfrog Avatar asked Dec 04 '09 00:12

openfrog


2 Answers

I don't know what is in the CDP that Steve referenced but basically the Objective-C Class Cluster is a construct that supports implementing the abstract Factory pattern.

The idea is simple: You want to provide a Factory (Cluster) interface that, with minimal description, manufactures and returns a specific concrete instance of a Factory Object that satisfies the behavior of the cluster family described by the Factory (Cluster) interface.

A simple concrete example: This example provides a Laugh factory that produces concrete classes of specific laughter types (e.g. Guffaw, Giggle). Pay attention to the Laugh initWithLaughter: method.

In Laugh.h:

#define kLaughWithGuffaw  1 #define kLaughWithGiggle  2  @interface Laugh: NSObject {} - (Laugh *) initWithLaughter:(NSUInteger) laughterType; - (void) laugh; @end 

In Laugh.m:

@interface Guffaws:Laugh {} - (void) laugh; @end  @interface Giggles:Laugh {} - (void) laugh; @end  @implementation Laugh - (Laugh *) initWithLaughter:(NSUInteger) laugherType {     id instanceReturn=nil;     ; // Removed for ARC [self release]     if ( laughterType == kLaughWithGuffaw )         instanceReturn = [[Guffaws alloc]init];     else if( laughterType == kLaughWithGiggle )         instanceReturn = [[Giggles alloc]init];     else         ; // deal with this     return instanceReturn; }  - (void) laugh {     NSLog(@"Humbug"); } @end  @implementation Guffaws     - (void) laugh {         NSLog(@"OH HA HA HOWAH HA HA HA");     } @end  @implementation Giggles     - (void) laugh {         NSLog(@"Tee hee");     } @end 
like image 135
Frank C. Avatar answered Oct 02 '22 01:10

Frank C.


From Apple's docs.... In short it's a design pattern used in the Foundation framework, which is probably why it's not mentioned in ObjC books.

A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way provides a simplified interface to the user, who sees only the publicly visible architecture.

like image 28
echo Avatar answered Oct 02 '22 02:10

echo