Source Address: http://limboy.me/ios/2014/01/04/class-cluster.html
Class cluster is a design pattern that is widely used in the foundation framework, for a simple example
NSArray *arr = [NSArray arrayWithObjects:@"foo",@"bar", nil];NSLog(@"arr class:%@", [arr class]);// output: __NSArrayI
Obviously __NSArrayI a private class to look at the header file for this class
@interface __NSArrayI : NSArray { unsigned int _used;}//...
Can see __NSArrayI the inheritance NSArray . Why do you design this? Take NSNumber For example, we all know that NSNumber can store many types of numbers, such as int/float/double and so on, one way is to use NSNumber as the base class, and then separate to implement their own subclasses, like this:
It doesn't seem to matter at first, but if there are a lot of subclasses, like this:
This is obviously not convenient enough for users to remember so many classes. If you use a class family, the problem becomes simple, with number as the abstract base class, subclasses implementing the Access method, and then defining multiple initializations in the base class, like this:
Now you just have to remember a class. NSNumberthe initialization pseudo-code might look like this:
- (id)initWithBool{ return [[__NSCFBoolean alloc]init];}- (id)initWithLong{ return [[__NSCFNumber alloc]init];}//...
Applications in iOS projects
When you develop an app, you often experience performance and behavior exactly the same, but the data source is different. As an example of a petal app, it's also a waterfall stream, probably from my favorite picture, a picture under one of the Artboards, a user's picture, and so on. If you create a new controller for each representation and initialize it with this controller, you will encounter the first problem: too many subclasses and inconvenient use. This can be done conveniently through class clusters. such as this:
@implementationHbwaterfallviewcontroller-(Id)Initwithliked{Return[[HblikedviewcontrollerAlloc]Init];}-(Id)Initwithboardid:(Nsinteger)Boardid{Return[[HbboardviewcontrollerAlloc]Initwithboardid:Boardid];}#pragma mark-General method-(psuicollectionviewcell *) collectionview: (psuicollectionview *) CollectionView cellforitematindexpath: (nsindexpath *) indexpath{//... }//... #pragma mark-the method that each subclass needs to implement - (void ) fetchmoredata{nsassert (no@ "subclasses need to implement this method" );
Used like this [[HBWaterfallViewController alloc]initWithBoardID:9527] or [[HBWaterfallViewController alloc]initWithLiked] . If there is a new datasource, add a new initialization method, for the user, open the header file, look at the beginning of the Init method is OK.
For example, now many applications need to take into account both iOS6 and iOS7, in the performance of different systems to load different picture resources, the most simple and brutal method is a variety of if/else judgments, such as:
if ([[UIDevice currentDevice]systemMajorVersion] < 7){ /* iOS 6 and previous versions */}else{ /* iOS 7 and above */}
Not elegant enough, you can use the idea of a class cluster to get rid of if/else judgments, put code that is not related to the view-specific elements in the base class, split the system version-dependent code into two subclasses, and then load the corresponding resources in the respective classes.
/* TestView.h */@interfaceTestView:UIView/* Common Method */-(void)Test;@end/* TESTVIEW.M */@implementationTestView+(Id)Alloc{If([SelfClass]==[TestViewClass]){If([[UideviceCurrentdevice]Systemmajorversion] < 7) { return [testviewios6 alloc} else {return [ TestViewIOS7 alloc; } } else {return [ super alloc; }}- (void ) Span class= "NF" >test{} @end
allocThe class is not returned here TestView , but is returned based on the system version TestViewIOS6 or TestViewIOS7 .
/* TESTVIEWIOS6.M */@implementation TestViewIOS6: testview- (void)drawrect: (cgrect) Rect{/ * Custom iOS6 Drawing code */}@end/* TESTVIEWIOS7.M */@implementation TestViewIOS7- (void)drawrect: (cgrect)rect{/ * Custom iOS7 Drawing code */}@end
Summary
The nature of a cluster is actually an abstract factory, and a class cluster can have multiple base classes, such as, the NSArray NSMutableArray latter being the former. It often has a good effect on some "similar" problems.
Reference
- Cocoa Core Competencies
- Strategies to support iOS7 UI
- Class Cluster Concepts in Objective-c
iOS 8: "Turn" cluster in iOS development application