A class Cluster is a collection of classes that define the same interface and provide the same functionality, and only the abstract class that exposes the interface can also be called the public class of a class family, and the interface of each concrete class is abstracted by the interface of the public class and hidden inside the cluster. These classes are generally not straightforward to use, and are generally implemented by subclasses of public classes, which can be called private subclasses. Look at the concept will feel a bit dull, in fact, the most commonly used NSString is a class cluster, but it does not seem obvious, more obvious is NSNumber,numberwithint,numberwithdouble, These methods actually invoke the NSNumber when the parent class instantiates the private subclass, returning the corresponding instance.
Basic Concepts
The concept is generally more boring, we can first write a simple test applet to see what the NSString cluster looks like:
NSString *[email protected] "blog Park-flyelephant"; Classclusterlog (result); Classclusterlog ([Result stringbyappendingstring:@ "original address: http://www.cnblogs.com/xiaofeixiang/"]); Classclusterlog ([nsstring stringwithutf8string: "---"]); Classclusterlog (Nshomedirectory ());
Output Method:
static void Classclusterlog (nsstring *str) { NSLog (@ "content=%@,class=%s,\tmember=%s,\tkind=%s \ n ", str, [Nsstringfromclass ([str class]) utf8string], [str ismemberofclass:[nsstring class]]?" Yes ":" NO ", [str iskindofclass:[nsstring class]]?" YES ":" NO ");}
Test results:
Through log we output the real type of the class, we find that each class type is different, and we memberclass to get the type of the class cluster, Kindclass can get the subclass type, in many cases the public class is implemented as an abstract class, Each method is implemented specifically in a subclass, so even if a subclass that inherits the public class is generated directly, it does not immediately produce the functionality the user wants.
Subclasses of a class cluster
The class cluster is a variety of categories to achieve abstraction, only the class cluster is visible outside of the public class, although the class cluster is the basic class implementation of the foundation framework, generally there is no need to generate subclasses, but sometimes the basic method of the cluster can not meet the basic requirements of the use of category to expand , this previous article has been described, if not very clear, you can check it. The class cluster contains the basic methods, but the specific implementations are in the private subclass. If we need to create a new subclass, we can directly implement the basic methods that have been exposed.
NSString has two basic methods: length and characteratindex, we define a NSString sub-class to see the effect:
@interface bitpattern:nsstring{ unsigned char value;} -(ID) Initwithchar: (char) val;-(Nsuinteger) length;-(Unichar) Characteratindex: (nsuinteger) index; @end
Implementation file:
@implementation bitpattern-(ID) Initwithchar: (char) val{ self=[super init]; if (self) { value=val; } return self;} -(Nsuinteger) length{ return 4;} -(Unichar) Characteratindex: (Nsuinteger) index{ return ' K ';} @end
Simple invocation:
NSString *bits; Bits=[[bitpattern Alloc]initwithchar: ' A ']; NSLog (@ "%@--Result:%s", [bits class],[bits utf8string]);
The final result is kkkk, and there are several areas to consider for implementing subclasses of class clusters:
1. Determine the private data structure, as the superclass can not use all the information structures;
2. Define the initialization method, define INIT: Such initialization method cannot inherit and use the initialization method of super class outside of Init;
3. Define constructors, cannot inherit and use the same method of superclass;
4. Defining Basic methods
5. Define other methods, by defining the underlying method, the method of public class declaration can be executed for a while, but the characteristics of the generated data structure may produce more efficient methods;
iOS Development-Cluster (class Cluster)