3 回答

TA貢獻1815條經驗 獲得超10個贊
從蘋果公司的文檔......。簡而言之,這是Foundation框架中使用的設計模式,這可能就是為什么ObjC書籍中未提及它的原因。
類集群是一種將公共,抽象超類下的多個私有,具體子類分組的體系結構。以這種方式對類進行分組為用戶提供了簡化的界面,該用戶只能看到公開可見的體系結構。

TA貢獻1911條經驗 獲得超7個贊
我不知道Steve所引用的CDP中有什么內容,但基本上,Objective-C類集群是一種支持實現抽象Factory模式的構造。
這個想法很簡單:您想要提供一個Factory(集群)接口,該接口以最少的描述即可制造并返回Factory對象的特定具體實例,該實例滿足Factory(集群)接口描述的集群家族的行為。
一個簡單的具體示例:此示例提供了一個Laugh工廠,該工廠產生特定笑聲類型(例如Guffaw,Giggle)的具體類。注意Laugh initWithLaughter:方法。
在Laugh.h中:
#define kLaughWithGuffaw 1
#define kLaughWithGiggle 2
@interface Laugh: NSObject {}
- (Laugh *) initWithLaughter:(NSUInteger) laughterType;
- (void) laugh;
@end
在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
- 3 回答
- 0 關注
- 747 瀏覽
添加回答
舉報