[Objective-C语言教程]复合对象(33)

在Objective-C中,可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类。 这些类对象是复合对象。你可能想知道什么是类集群,下面首先了解什么是类集群。

1. 类集群

类集群是基础框架广泛使用的设计模式。 类集群在公共抽象超类下组合了许多私有具体子类。 以这种方式对类进行分组简化了面向对象框架的公开可见体系结构,而不会降低其功能丰富性。 类集群基于抽象工厂设计模式。

为了简单起见,创建了一个基于输入值处理它的单个类,而不是为类似的函数创建多个类。

例如,在NSNumber中有许多类的集群,如charintbool等。将它们全部组合到一个类中,该类负责处理单个类中的类似操作。 NSNumber实际上将这些原始类型的值包装到对象中。

2. 什么是复合对象?

通过在设计的对象中嵌入私有集群对象,创建一个复合对象。 此复合对象可以依赖于集群对象的基本功能,仅拦截复合对象希望以某种特定方式处理的消息。 此体系结构减少了必须编写的代码量,并允许利用Foundation Framework提供的测试代码。

如下图中解释 -

复合对象必须声明自己是集群的抽象超类的子类。 作为子类,它必须覆盖超类的原始方法。 它也可以覆盖派生方法,但这不是必需的,因为派生方法通过原始方法工作。

NSArray类的count方法就是一个例子; 介入对象的覆盖方法的实现可以实现简单如下 -

1 - (unsigned)count  {
2    return [embeddedObject count];
3 }

在上面的例子中,嵌入对象实际上是NSArray类型。

复合对象示例

现在,为了看到一个完整的示例,请看下面给出的Apple文档中的示例。

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface ValidatingArray : NSMutableArray {
 4    NSMutableArray *embeddedArray;
 5 }
 6 
 7 + validatingArray;
 8 - init;
 9 - (unsigned)count;
10 - objectAtIndex:(unsigned)index;
11 - (void)addObject:object;
12 - (void)replaceObjectAtIndex:(unsigned)index withObject:object;
13 - (void)removeLastObject;
14 - (void)insertObject:object atIndex:(unsigned)index;
15 - (void)removeObjectAtIndex:(unsigned)index;
16 
17 @end
18 
19 @implementation ValidatingArray
20 - init {
21    self = [super init];
22    if (self) {
23       embeddedArray = [[NSMutableArray allocWithZone:[self zone]] init];
24    }
25    return self;
26 }
27 
28 + validatingArray {
29    return [[self alloc] init] ;
30 }
31 
32 - (unsigned)count {
33    return [embeddedArray count];
34 }
35 
36 - objectAtIndex:(unsigned)index {
37    return [embeddedArray objectAtIndex:index];
38 }
39 
40 - (void)addObject:(id)object {
41    if (object != nil) {
42       [embeddedArray addObject:object];
43    }
44 }
45 
46 - (void)replaceObjectAtIndex:(unsigned)index withObject:(id)object; {
47    if (index <[embeddedArray count] && object != nil) {
48       [embeddedArray replaceObjectAtIndex:index withObject:object];
49    }
50 }
51 
52 - (void)removeLastObject; {
53    if ([embeddedArray count] > 0) {
54       [embeddedArray removeLastObject];
55    }
56 }
57 
58 - (void)insertObject:(id)object atIndex:(unsigned)index; {
59    if (object != nil) {
60       [embeddedArray insertObject:object atIndex:index];
61    }
62 }
63 
64 - (void)removeObjectAtIndex:(unsigned)index; {
65    if (index <[embeddedArray count]) {
66       [embeddedArray removeObjectAtIndex:index];
67    }
68 }
69 
70 @end
71 
72 int main() {
73    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
74    ValidatingArray *validatingArray = [ValidatingArray validatingArray];
75 
76    [validatingArray addObject:@"Object1"];
77    [validatingArray addObject:@"Object2"];
78    [validatingArray addObject:[NSNull null]];
79    [validatingArray removeObjectAtIndex:2];
80    NSString *aString = [validatingArray objectAtIndex:1];
81    NSLog(@"The value at Index 1 is %@",aString);
82    [pool drain];
83 
84    return 0;
85 }

执行上面示例代码,得到以下结果:

扫描二维码关注公众号,回复: 5608503 查看本文章
2018-11-28 22:03:54.124 main[3247] The value at Index 1 is Object2

在上面的例子中,可以看到验证数组的一个函数不允许添加会导致正常情况下崩溃的空对象。 但验证数组负责处理它。 类似地,验证数组中的每个方法都添加了除正常操作序列之外的验证过程。

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10572193.html