views:

660

answers:

5

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?

+8  A: 

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.

echo
+1 for googling ;)
Georg Fritzsche
yeah, low-hanging fruit you know...
echo
roflmao. thanks. gonna google next time. thought if its not in my fat books, it can only be on your heads ;)
openfrog
+5  A: 

The NSArray class cluster isn't "heavyweight", it's a way for any number of implementations of an array class to be used without your code knowing or caring about the particular implementation. Under the hood, there are concrete subclasses of NSArray that are appropriate to different use cases, such as large, sparse arrays, or arrays containing a specific number of elements that are known at compile time.

NSArray, NSString, and NSNumber are the class clusters you'll most often encounter.

NSResponder
Ironically, in practice, only one concrete class per cluster is ever used anymore — NSCF{Array|String|Number} — and the implementation changes are internal to that class. That's as far as I know, anyway. Even NSArray and NSMutableArray instances show up the same class.
Chuck
+4  A: 

From programming in objective c by Stephen Kochan on page 498 in the glossary, cluster:

An abstract class that groups a set of private concrete subclasses, providing a simplified interface to the user through the abstract class.

t011phr33
+1 For quoting an answer from an Objective-C book, particularly given the original question. That's a good book, too.
Quinn Taylor
+4  A: 

For a guy who seems to need another book :-):

Cocoa Design Patterns contains a chapter dedicated to the Class Cluster Pattern.

--Steve

Steve
+2  A: 

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 initLaugher: method.

In Laugh.h:

#define kLaughWithGuffaw  1
#define kLaughWithGiggle  2

@interface Laugh: NSObject {}
- (Laugh *) initWithLaugher:(NSUInteger) laugherType;
- (void) laugh;
@end

In Laugh.m:

@interface Guffaws:Laugh {}
- (void) laugh;
@end

@interface Giggles:Laugh {}
- (void) laugh;
@end

@implementation Laugh
- (Laugh *) initWithLaugher:(NSUInteger) laugherType {
    id instanceReturn=nil;
    [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

-- Frank

Frank C.