gpt4 book ai didi

objective-c - Objective-C 中所谓的 "Class Cluster"到底是什么?

转载 作者:IT老高 更新时间:2023-10-28 11:20:39 26 4
gpt4 key购买 nike

我在读到 NSArray 就是这样一个东西。听起来很沉重。我的办公 table 上有 7 本关于 Objective-C、Cocoa 和 C 的非常厚的书。它们都没有提到 Class Cluster,至少我在书后的索引中找不到。那是什么?

最佳答案

我不知道 Steve 引用的 CDP 中有什么,但基本上,Objective-C 类集群是一个支持实现抽象 Factory 的构造。模式。

想法很简单:您想提供一个工厂(集群)接口(interface),用最少的描述,制造并返回一个工厂对象的特定具体实例,满足Factory(Cluster)接口(interface)所描述的集群族的行为。

一个简单的具体示例:这个示例提供了一个 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

关于objective-c - Objective-C 中所谓的 "Class Cluster"到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1844158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com