目录

Objective-C - 复合对象(C - Composite Objects)

我们可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类。 这些类对象是复合对象。 所以你可能想知道什么是类集群。 所以我们将首先看到什么是类集群。

类集群

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

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

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

什么是复合对象?

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

这在下图中解释。

Objective-C复合对象

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

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

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

↑回到顶部↑
WIKI教程 @2018