Objective-C Composite Objects.


Please explain why do you think this question should be reported?

Report Cancel

We can create subclass within a class cluster that defines a class that embeds within it an object. These class objects are composite objects.

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.

Objective-C Composite Objects

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.

Please follow and like us:

About the Author