IOS cluster (class Cluster) Usage tips

Source: Internet
Author: User

We all know that the use of clusters in iOS is very common, such as NSNumber, NSString, Nsarray, and so on are all clusters. We nsnumber for example, for int,bool, unsigned int and so on data types, how do we encapsulate them in the form of classes? In general, we might think that for each data type to be individually encapsulated into a class, such as an int type we can do a nsint class, and so on. This is the right thing to think, but let's think about what's wrong with that. As the number of data types that need to be supported grows and these classes become more and more relevant, we need to remember more and more classes for developers. So is there a good plan? Haha, this time the cluster is shining on the debut.
The problem we have now is that there are more and more similar classes that require too much memory for the developer. Obviously, the direction of our problem-solving is definitely toward reducing the number of classes exposed to the developer in this direction. Let us recall the design pattern, abstract Factory mode is not to do this thing!
For NSNumber, we use a nsnumber to act as a factory for native data types such as Int,bool,long, defining a bunch of factory methods externally, and then transferring to the corresponding internal classes like Nsint to implement concrete logic within NSNumber. In this way, the problems we encounter have been solved perfectly.
Let's implement a specific class cluster by ourselves. The scene is like this, I now have an app that only supports the English version, and now I want it to also support the Chinese and Japanese versions, and so on. Let's look at the model class first!

@interface PowerUpDetail : WMModelObject@property (nonatomic, copy)     NSString* name;@property (nonatomic, copy)     NSString* desc;@end

The name and desc are the corresponding fields in the English version, and now I want to implement the Chinese version and expand the structure for other languages later.
Now there are several scenarios:
1. Inherit Powerupdetail, implement subclasses POWERUPDETAILCN and Powerupdetailen where all calls to Powerupdetail, Distinguish whether you should call POWERUPDETAILCN or Powerupdetailen.
2. The powerupdetail is made into a class cluster, exposing the universal interface externally and transferring internally to the inner class to realize the concrete logic.

Scenario 1 is the easiest to think of, but its disadvantage is that there is a need to modify the existing calling code, and if in the future as more and more languages are supported, these classes will be very much, where the call requires a bunch of if-else statements to differentiate, the code is ugly, the workload is very large.
Scenario 2 is a good solution to minimize exposure to external exposures, where all calls can be made without modification, and all modifications can be done only in the Powerupdetail class.
Below the code ~ (≧▽≦)/~ la La
In the PowerUpDetail.h file:

@interface PowerUpDetail : WMModelObject@property (nonatomic, copy)     NSString* name;@property (nonatomic, copy)     NSString* desc;- (NSString *)localizedName;- (NSString *)localizedDesc;@end

In the powerupdetail.m file:

#pragma mark-for 中文版 Version @interface _powerupdetailen : powerupdetail @end @implementation _powerupdetailen @end#pragma mark-for Chinese Version @interface _powerupdetailch : powerupdetail @property(nonatomic, copy)NSString*namech;@property(nonatomic, copy)NSString*DESCCH;@end @implementation _powerupdetailch -(Instancetype) Initwithdictionary: (nsdictionary*) dic{if( Self= [SuperInitwithdictionary:dic]) {_namech = [[DiC objectforkey:@"Namech"] copy]; _DESCCH = [[DiC objectforkey:@"DESCCH"] copy]; }return  Self;} - (NSString*) localizedname{return  Self. Namech;} - (NSString*) localizeddesc{return  Self. DESCCH;}@end#pragma mark-class Cluster @implementation powerupdetail + (Instancetype) alloc{if([ SelfClass] = = [Powerupdetail class]) {if(wmischinesepreference) {return[_powerupdetailch alloc]; }Else{return[_powerupdetailen alloc]; }    }Else{return[SuperALLOC]; }}-(Instancetype) Initwithdictionary: (nsdictionary*) dic{if( Self= [SuperInitwithdictionary:dic]) {_name = [[DiC objectforkey:@"Name"] copy]; _desc = [[DiC objectforkey:@"desc"] copy]; }return  Self;} - (NSString*) localizedname{return  Self. Name;} - (NSString*) localizeddesc{return  Self. Desc;}@end

Here, I implemented two inner classes _powerupdetailch and _powerupdetailen, all of them inherited from Powerupdetail, and Powerupdetail exposed to the outside two methods:
-(NSString *) localizedname;
-(NSString *) Localizeddesc;
These methods can be replicated in subclasses as needed.
Special attention needs to be paid to the Alloc method of cluster Powerupdetail, where the if ([self class] = = [Powerupdetail class]) condition is used, otherwise there will be a dead loop, because you should be able to see it easily, I will not be wordy!

+ (instancetype)alloc{    if ([selfclass] == [PowerUpDetail class]) {        if (WMIsChinesePreference) {            return [_PowerUpDetailCH alloc];        else {            return [_PowerUpDetailEN alloc];        }    else {        return [super alloc];    }}

OK, I'm done, the cluster is actually very simple, it is an abstract Factory mode version of the iOS implementation.
In addition, it is recommended to read this article "Cluster in iOS development application", I also learned from this article knowledge.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS cluster (class Cluster) Usage tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.