Objective-C Composite Objects
We can create subclass within a class cluster that defines a class that embeds within it an object. These class objects are composite objects.
So you might be wondering what's a class cluster. So we will first see what's a class cluster.
Class Clusters
Class clusters are a design pattern that the Foundation framework makes extensive use of. Class clusters group a number of private concrete subclasses under a public abstract superclass. The grouping of classes in this way simplifies the publicly visible architecture of an object-oriented framework without reducing its functional richness. Class clusters are based on the Abstract Factory design pattern.
To make it simple, instead of creating multiple classes for similar functions, we create a single class that will take care of its handling based on the value of input.
For example, in NSNumber we have many clusters of classes like char, int, bool and so on. We group all of them to a single class that takes care of handling the similar operations in a single class. NSNumber actually wraps the value of these primitive types into objects.
So what's exactly composite object?
By embedding a private cluster object in an object of our own design, we create a composite object. This composite object can rely on the cluster object for its basic functionality, only intercepting messages that the composite object wants to handle in some particular way. This architecture reduces the amount of code we must write and lets you take advantage of the tested code provided by the Foundation Framework.
This is explained in the following figure.

The composite object must declare itself to be a subclass of the cluster's abstract superclass. As a subclass, it must override the superclass' primitive methods. It can also override derived methods, but this isn't necessary because the derived methods work through the primitive ones.
The count method of the NSArray class is an example; the intervening object's implementation of a method it overrides can be as simple as:
- (unsigned)count
{
return [embeddedObject count];
}
In the above example, embedded object is actually of type NSArray.
A Composite Object example
Now in order to see a complete example, let's look at the example from the Apple documentation which is given below.
#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] > ) {
[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:];
NSString *aString = [validatingArray objectAtIndex:];
NSLog(@"The value at Index 1 is %@",aString);
[pool drain];
return ;
}
Now when we compile and run the program, we will get the following result.
-- ::54.294 demo[] The value at Index is Object2
In the above example, we can see that validating array's one function would not allow adding null objects that will lead to crash in the normal scenario. But our validating array takes care of it. Similarly, each of the method in validating array adds validating processes apart from the normal sequence of operations.
Objective-C Composite Objects的更多相关文章
- Composite Design Pattern in Java--转
https://dzone.com/articles/composite-design-pattern-in-java-1 The composite pattern is meant to &quo ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- linux内核的makefile.txt讲解
linux内核的linux-3.6.5\Documentation\kbuild\makefiles.txt Linux Kernel Makefiles This document describe ...
- Linux内核Makefile文件(翻译自内核手册)
--译自Linux3.9.5 Kernel Makefiles(内核目录documention/kbuild/makefiles.txt) kbuild(kernel build) 内核编译器 Thi ...
- SOLID architecture principles using simple C# examples
转:http://www.codeproject.com/Articles/703634/SOLID-architecture-principles-using-simple-Csharp?msg=4 ...
- CG&Game资源(转)
cg教程下载: http://cgpeers.com http://cgpersia.com http://bbs.ideasr.com/forum-328-1.html http://bbs.ide ...
- Kbuild文件
3 Kbuild文件 大部分内核中的Makefile都是使用Kbuild组织结构的Kbuild Makefile.这章将介绍Kbuild Makefile的语法. 对于Kbuild文件名来讲,Kbui ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
随机推荐
- SQL编程题-----1
首先,题目给出这个数据库表格 要求写出SQL语句使之变成如下表格 解决方法: SELECT t1.Rq,t1.胜,t2.负 FROM //t1和t2是自己命的新表格的名字 (SELEC ...
- PhpStrom之添加文件夹至左侧目录树
1.打开编辑器,点击工具栏 File,并选择Open (File -> Open) 2.选择需要添加的文件夹路径,点击 OK 3.点击OK后弹出下图窗口(第一个选项:Open in new wi ...
- win7 mongod不是内部命令
1.下载MongoDB 1.1 MongoDB下载 1.2 选择Server下面的 Community 2.安装MongoDB 2.1 注意事项:一直下一步就行了,但是遇到下面这个界面,注意一定要去掉 ...
- jQuery 设置图片 src 的2种方法
// 方法1 $('#imgValidateCode').attr("src", data.CodeUrl); // 方法2 var self = $("#refresh ...
- spring boot 使用Schedule创建轻量级定时任务
Scheduled SpringBoot配置定时任务可以直接使用自带的Scheduled,这相当于一个轻量级的Quartz,它可以让我们直接使用注解来完成定时任务的配置. Scheduled调度时间设 ...
- ADT版本查看,This Android SDK requires Andr...ate ADT to the latest问题
ADT版本查看 Help->About ADT This Android SDK requires Andr...ate ADT to the latest问题 这样的问题很好解决,一个升级AD ...
- C 语言实例 - 使用结构体(struct)
C 语言实例 - 使用结构体(struct) C 语言实例 C 语言实例 使用结构体(struct)存储学生信息. 实例 #include <stdio.h> struct student ...
- MySQL - 执行sql报错USING BTREE
问题与分析 在执行sql文件时发现报错如下: You have an error in your SQL syntax; check the manual that corresponds to yo ...
- 相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传
一.点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传 (1)上传头像属性 // 图片二进制格式 表单上传 @property (nonatomic, strong) ...
- 【bzoj1123】BLO
1123: [POI2008]BLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2222 Solved: 1090[Submit][Status ...