Home > DeveloperSection > Blogs > iOS Concept : Class Cluster

iOS Concept : Class Cluster

iPhone iPhone 
0 Comment(s)
 690  View(s)
Rate this:

iOS Concept : Class Cluster

Previously we learn Layers of iOS : iOS Technology Overview - Layers of iOS

 Class clusters are a design pattern that the Foundation framework makes extensive use of Class clusters group a number of private concrete sub classes under a public abstract super class. 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.A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass


A new class that you create within a class cluster must:


          Be a subclass of the cluster’s abstract superclass

          Declare its own storage

         Override all initializer methods of the superclass

         Override the superclass’s primitive methods


     Because the cluster’s abstract superclass is the only publicly visible node in the cluster’s hierarchy. The first point is obvious: This implies that the new subclass will inherit the cluster’s interface but no instance variables, because the abstract super class declares none.


Thus the second point: The subclass must declare any instance variables it needs.

Finally, the subclass must override any method it inherits that directly accesses an objects instance variables. Such methods are called primitive methods.


    Simple Concept but Complex Interface: A simple hierarchy for number classes:


Number is the abstract superclass that declares in its methods the operations common to its subclasses. However, it doesn’t declare an instance variable to store a number. The subclasses declare such instance variables and share in the programmatic interface declared by Number.


(One abstract public class declares the interface for multiple private subclasses.)


Class cluster architecture applied to number classes:


Users of this hierarchy see only one public class, Number, so how is it possible to allocate instances of the proper subclass? The answer is in the way the abstract superclass handles instantiation.


    For example, several of the common Cocoa classes are implemented as class clusters, including NSArray, NSString, and NSDictionary. There are many ways in which they can represent their internal data storage. For any particular instance, the abstract class chooses the most efficient class to use based on the data that instance is being initialized with.


    Taking the Foundation framework’s NSString class as an example, you could create three different string objects:


NSString *string1 = @"UTF32.txt";

NSString *string2 = [NSHomeDirectory() stringByAppendingPathComponent:string1];

NSTextStorage *storage = [[NSTextStorage alloc] init   WithString:string2];

NSString *string3 = [storage string];


Each string may be an instance of a different private subclass. Although each of the objects is of a private subclass of NSString, it’s convenient to consider each of the objects to be instances of the NSString class. You use the instance methods declared by NSString just as you would if they were instances of NSString itself.


    Benefits: The benefit of a class cluster is primarily efficiency. The internal representation of the data that an instance manages can be tailored to the way it’s created or being used. Moreover, the code you write will continue to work even if the underlying implementation changes.



Next, we will learn about iOS Concept : Delegate


Don't want to miss updates? Please click the below button!

Follow MindStick