- 基本 Objective-C
- Objective-C - 主页
- Objective-C - 概述
- Objective-C - 环境设置
- Objective-C - 程序结构
- Objective-C - 基本语法
- Objective-C - 数据类型
- Objective-C - 变量
- Objective-C - 常量
- Objective-C - 运算符
- Objective-C - 循环
- Objective-C - 决策
- Objective-C - 函数
- Objective-C - 块
- Objective-C - 数字
- Objective-C - 数组
- Objective-C - 指针
- Objective-C - 字符串
- Objective-C - 结构
- Objective-C - 预处理器
- Objective-C - Typedef
- Objective-C - 类型转换
- Objective-C - 日志处理
- Objective-C - 错误处理
- 命令行参数
- 高级 Objective-C
- Objective-C - 类和对象
- Objective-C - 继承
- Objective-C - 多态性
- Objective-C - 数据封装
- Objective-C - 类别
- Objective-C - 摆姿势
- Objective-C - 扩展
- Objective-C - 协议
- Objective-C - 动态绑定
- Objective-C - 复合对象
- Obj-C - 基础框架
- Objective-C - 快速枚举
- Obj-C - 内存管理
- Objective-C 有用资源
- Objective-C - 快速指南
- Objective-C - 有用的资源
- Objective-C - 讨论
Objective-C 复合对象
我们可以在类簇中创建子类,该子类定义了一个在其中嵌入对象的类。这些类对象是复合对象。所以您可能想知道什么是类簇。所以我们首先来看看什么是类簇。
类簇
类集群是基础框架广泛使用的一种设计模式。类簇将许多私有具体子类分组到公共抽象超类下。以这种方式对类进行分组简化了面向对象框架的公开可见的体系结构,而不会降低其功能的丰富性。类簇基于抽象工厂设计模式。
为了简单起见,我们没有为类似的函数创建多个类,而是创建一个类,该类将根据输入的值进行处理。
例如,在 NSNumber 中,我们有许多类簇,如 char、int、bool 等。我们将它们全部分组到一个类中,该类负责处理单个类中的类似操作。NSNumber 实际上将这些原始类型的值包装成对象。
什么是复合对象?
通过将私有集群对象嵌入到我们自己设计的对象中,我们创建了一个复合对象。该复合对象可以依赖集群对象来实现其基本功能,仅拦截复合对象想要以某种特定方式处理的消息。这种架构减少了我们必须编写的代码量,并允许您利用基础框架提供的经过测试的代码。
下图对此进行了解释。
复合对象必须声明自己是集群抽象超类的子类。作为子类,它必须重写超类的原始方法。它还可以重写派生方法,但这不是必需的,因为派生方法通过原始方法工作。
NSArray类的count方法就是一个例子;中间对象对其重写的方法的实现可以简单如下:
- (unsigned)count {
return [embeddedObject count];
}
在上面的例子中,嵌入的对象实际上是NSArray类型。
复合对象示例
现在为了查看完整的示例,让我们看一下下面给出的 Apple 文档中的示例。
#import <Foundation/Foundation.h>
@interface ValidatingArray : NSMutableArray {
NSMutableArray *embeddedArray;
}
+ validatingArray;
- init;
- (unsigned)count;
- objectAtIndex:(unsigned)index;
- (void)addObject:object;
- (void)replaceObjectAtIndex:(unsigned)index withObject:object;
- (void)removeLastObject;
- (void)insertObject:object atIndex:(unsigned)index;
- (void)removeObjectAtIndex:(unsigned)index;
@end
@implementation ValidatingArray
- init {
self = [super init];
if (self) {
embeddedArray = [[NSMutableArray allocWithZone:[self zone]] init];
}
return self;
}
+ validatingArray {
return [[self alloc] init] ;
}
- (unsigned)count {
return [embeddedArray count];
}
- objectAtIndex:(unsigned)index {
return [embeddedArray objectAtIndex:index];
}
- (void)addObject:(id)object {
if (object != nil) {
[embeddedArray addObject:object];
}
}
- (void)replaceObjectAtIndex:(unsigned)index withObject:(id)object; {
if (index <[embeddedArray count] && object != nil) {
[embeddedArray replaceObjectAtIndex:index withObject:object];
}
}
- (void)removeLastObject; {
if ([embeddedArray count] > 0) {
[embeddedArray removeLastObject];
}
}
- (void)insertObject:(id)object atIndex:(unsigned)index; {
if (object != nil) {
[embeddedArray insertObject:object atIndex:index];
}
}
- (void)removeObjectAtIndex:(unsigned)index; {
if (index <[embeddedArray count]) {
[embeddedArray removeObjectAtIndex:index];
}
}
@end
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ValidatingArray *validatingArray = [ValidatingArray validatingArray];
[validatingArray addObject:@"Object1"];
[validatingArray addObject:@"Object2"];
[validatingArray addObject:[NSNull null]];
[validatingArray removeObjectAtIndex:2];
NSString *aString = [validatingArray objectAtIndex:1];
NSLog(@"The value at Index 1 is %@",aString);
[pool drain];
return 0;
}
现在,当我们编译并运行该程序时,我们将得到以下结果。
2013-09-28 22:03:54.294 demo[6247] The value at Index 1 is Object2
在上面的示例中,我们可以看到验证数组的 one 函数不允许添加空对象,否则在正常情况下会导致崩溃。但我们的验证数组会处理它。类似地,验证数组中的每个方法都在正常操作序列之外添加了验证过程。