Composite Objects -Objective C
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 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 cluster group a number of concrete subclasses under a public abstract superclass. The grouping of claases 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 creates a single class that will take care of its handling based on the values of input.
For example, n NSNumber we have many clusters of classes like char, int , bool and so on. We group all of them to a single class that take care of handling the similar operations in a single class. NSNumber actually wraps the value of these primitive type info 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 for its basic functionality ,only intercepting message that the composite objects 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 overrider the superclass primitive methods. It can also override derived methods, but this isn't necessary because the dereived methods 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:
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:
So you might be wondering what's 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 cluster group a number of concrete subclasses under a public abstract superclass. The grouping of claases 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 creates a single class that will take care of its handling based on the values of input.
For example, n NSNumber we have many clusters of classes like char, int , bool and so on. We group all of them to a single class that take care of handling the similar operations in a single class. NSNumber actually wraps the value of these primitive type info 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 for its basic functionality ,only intercepting message that the composite objects 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 overrider the superclass primitive methods. It can also override derived methods, but this isn't necessary because the dereived methods 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.{
return [embeddedObject count];
}
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:
In above example we can see that validating array's one function would not allow adding null object that will lead to crash in the normal scenario. But our validatin array takes care of it. Similarly each of the method in validating array adds validating process part from the normal sequence of operation.
No comments: