Ios---class clusters

Source: Internet
Author: User
<span id="Label3"></p><p><p>We all know that the use of clusters in iOS is very common, such as <code class="prettyprint">NSNumber</code> , and <code class="prettyprint">NSString</code> <code class="prettyprint">NSArray</code> so on, are Clusters. For <code class="prettyprint">NSNumber</code> example, for int,bool, unsigned int, and so on, how do we encapsulate them in the form of a class? 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.</p></p><p><p>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!</p></p><p><p>For <code class="prettyprint">NSNumber</code> , we use a <code class="prettyprint">NSNumber</code> factory that is a native data type such as int,bool,long, define a bunch of factory methods externally, and <code class="prettyprint">NSNumber</code> internally transfer to the corresponding <code class="prettyprint">NSInt</code> inner class to implement concrete Logic. In this way, the problems we encounter have been solved perfectly.</p></p><p><p>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!</p></p><pre class="prettyprint ruby"><pre class="prettyprint ruby"><code><span class="variable">@interface <span class="constant">PowerUpDetail <span class="symbol">: <span class="constant">WMModelObject<span class="variable">@property (nonatomic, copy) <span class="constant">NSString* name;<span class="variable">@property (nonatomic, copy) <span class="constant">NSString* desc;<span class="variable">@end</span></span></span></span></span></span></span></span></span></code></pre></pre><p><p>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.</p></p><p><p>Now there are several scenarios:</p></p><pre class="prettyprint undefined"><pre class="prettyprint undefined"><code>1. 继承PowerupDetail,实现子类PowerupDetailCN和PowerupDetailEN在所有调用PowerupDetail的地方,区分出到底应该调用PowerupDetailCN还是PowerupDetailEN。2. 把PowerupDetail做成类簇,对外暴露通用接口,对内转调给内部类来实现具体逻辑。</code></pre></pre><p><p>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.</p></p><p><p>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.</p></p><p><p>Below the code ~ (≧▽≦)/~ La La</p></p><p><p>In the PowerUpDetail.h file:</p></p><pre class="prettyprint perl"><pre class="prettyprint perl"><code><span class="variable">@interface PowerUpDetail : WMModelObject<span class="variable">@property (nonatomic, copy) NSString* name;<span class="variable">@property (nonatomic, copy) NSString* desc;- (NSString <span class="variable">*)localizedName;- (NSString <span class="variable">*)localizedDesc;<span class="variable">@end</span></span></span></span></span></span></code></pre></pre><p><p>In the POWERUPDETAIL.M file:</p></p><pre class="prettyprint ruby"><code><span class="comment">#pragma mark-for 中文版 Version<span class="variable">@interface _powerupdetailen<span class="symbol">:<span class="constant">Powerupdetail<span class="variable">@end<span class="variable">@implementation _powerupdetailen<span class="variable">@end<span class="comment">#pragma mark-for Chinese Version<span class="variable">@interface _powerupdetailch<span class="symbol">:<span class="constant">Powerupdetail<span class="variable">@property (nonatomic, Copy)<span class="constant">NSString *namech;<span class="variable">@property (nonatomic, Copy)<span class="constant">NSString *descch;<span class="variable">@end<span class="variable">@implementation _powerupdetailch-(instancetype)<span class="symbol">Initwithdictionary: (<span class="constant">Nsdictionary *) dic{<span class="indent"> <span class="keyword">if (<span class="keyword">Self = [<span class="keyword">Super<span class="symbol">initwithdictionary:dic]) {<span class="indent"> <span class="indent">_namech = [[dic<span class="symbol">objectforkey:@<span class="string">"namech"] copy];<span class="indent"> <span class="indent">_DESCCH = [[dic<span class="symbol">objectforkey:@<span class="string">"descch"] copy];<span class="indent">}<span class="indent"> <span class="keyword">Return<span class="keyword">self;} - (<span class="constant">NSString *) localizedname{<span class="indent"> <span class="keyword">Return<span class="keyword">self.namech;} - (<span class="constant">NSString *) localizeddesc{<span class="indent"> <span class="keyword">Return<span class="keyword">self.descch;}<span class="variable">@end<span class="comment">#pragma mark-class Cluster<span class="variable">@implementation<span class="constant">powerupdetail+ (instancetype) alloc{<span class="indent"> <span class="keyword">If ([<span class="keyword">Self<span class="keyword">class] = = [<span class="constant">Powerupdetail<span class="keyword">Class]) {<span class="indent"> <span class="indent"> <span class="keyword">if (<span class="constant">Wmischinesepreference) {<span class="indent"> <span class="indent"> <span class="indent"> <span class="keyword">return [_powerupdetailch alloc];<span class="indent"> <span class="indent">}<span class="keyword">else {<span class="indent"> <span class="indent"> <span class="indent"> <span class="keyword">return [_powerupdetailen alloc];<span class="indent"> <span class="indent">}<span class="indent">}<span class="keyword">else {<span class="indent"> <span class="indent"> <span class="keyword">return [<span class="keyword">Super alloc];<span class="indent">}}-(instancetype)<span class="symbol">Initwithdictionary: (<span class="constant">Nsdictionary *) dic{<span class="indent"><span class="indent"> <span class="keyword">if (<span class="keyword">self = [<span class="keyword">super <span class="symbol">initwithdictionary:dic]) {<span class="indent"> <span class="indent"> _name = [[dic <span class="symbol">objectforkey:@<span class="string"> "name"] copy]; <span class="indent"> <span class="indent"> _desc = [[dic <span class="symbol">objectforkey:@<span class="string"> " Desc "] copy]; <span class="indent">}<span class="indent"> <span class="keyword">return <span class="keyword">self; -(<span class="constant">nsstring *) localizedname{<span class="indent"> <span class="keyword">return <span class=" Keyword ">self.name;} -(<span class="constant">nsstring *) localizeddesc{<span class="indent"> <span class="keyword">return <span class=" Keyword ">self.desc;} <span class="variable"> @end </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p>here, I implemented two inner classes <code class="prettyprint">_PowerUpDetailCH</code> and <code class="prettyprint">_PowerUpDetailEN</code> , they all inherit from, and <code class="prettyprint">PowerUpDetail</code> <code class="prettyprint">PowerUpDetail</code> exposed to the outside two methods:</p></p><pre class="prettyprint perl"><pre class="prettyprint perl"><code> <span class="variable">*)localizedName; - (NSString <span class="variable">*)localizedDesc;</span></span></code></pre></pre><p><p>These methods can be replicated in subclasses as Needed.</p></p><p><p>Special attention needs to be paid to the Alloc method of class clusters, <code class="prettyprint">PowerUpDetail</code> where the use of the If ([self class] = = [powerupdetail Class]) condition is used, otherwise there will be a cycle of death, because you should be able to see it easily, I will not wordy!</p></p><pre class="prettyprint ruby"><code>+ (instancetype) alloc{<span class="indent"> <span class="keyword">If ([<span class="keyword"><span class="keyword">self <span class="keyword">class] = = [<span class="constant">powerupdetail <span class="keyword">class]) {<span class="indent"> <span class="indent"> <span class="keyword">if (<span class="constant"> Wmischinesepreference) {<span class="indent"> <span class="indent"> <span class="indent"> <span class="keyword"> return [_powerupdetailch alloc]; <span class="indent"> <span class="indent">} <span class="keyword">else {<span class="indent"> <span class="indent"> <span class="indent"> <span class="keyword">return [_powerupdetailen alloc];<span class="indent"> <span class="indent">}<span class="indent">} <span class="keyword">else {<span class="indent"> <span class="indent"> <span class="keyword">return [<span class="keyword">super alloc];<span class="indent">}} </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p>OK, I'm done, The cluster is actually very simple, it is an abstract Factory mode version of the iOS Implementation.</p></p><p><p>Ios---class clusters</p></p></span>

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.