Single case design pattern in OC and macro extraction of single case

Source: Internet
Author: User

1 //when an object needs to be reused and is frequent, you can use the singleton design pattern for an object2 //The design of a single case is actually a multi-alloc internal allocwithzone, overriding the method3 4 #pragmaPerson.h file5 6 #import<Foundation/Foundation.h>7 @interfacePerson:nsobject <NSCopying,NSMutableCopying>8+ (instancetype) Shareperson;//provides a class factory method for classes that create singleton objects9 @endTen  One #pragmaPERSON.M file A  - // - //person.m the //Single case Design - // - //Created by Kang Sengchu on 15/11/26. - //Copyright (c) 2015 Thomson Qiu. All rights reserved. + // -  + #import "Person.h" A  at  -  - @implementation Person -  -+(instancetype) Shareperson - { inPerson *instance = [[Self alloc] init];//single-case design can be done from the Allocwithzone method within the Alloc -     returninstance; to } + StaticPerson *_instance =Nil; - //This method determines whether the address of the same storage space is created each time . the+ (Instancetype) Allocwithzone: (struct_nszone *) Zone * { $     //if _instance equals nil, then the alocwithzone of the parent class is called to create an object, and the initialization is assigned to it .Panax Notoginseng     if(_instance = =Nil) { -_instance =[[Super Allocwithzone:zone] init]; the     } +      A     //if _instance is not equal to nil, which is already pointing to the object, then return the address directly so that the newly created object also points to the object of the same address the     return_instance; + } -  $- (ID) Copywithzone: (Nszone *) Zone $ { -     //copy is called by an object, and in the case of an object already exists, you do not need to create the object, directly return the object pointer -     return_instance; the } - Wuyi- (ID) Mutablecopywithzone: (Nszone *) Zone the { -     //mutablecopy and copy, directly returning the object pointer can be Wu     return_instance; - } About  $  -  - //If you are in the MRC, you also need to rewrite release, retain, Retaincount --(OneWayvoid) Release A { +     //to ensure that the Singleton object is not released before the program ends, \ the I don't write anything here . - } $-(instancetype) retain the { the     //return a single object address directly the     return_instance; the } --(Nsuinteger) Retaincount in { the     //can return a special value to remind other programmers the //return maxfloat; About     returnUint64_max; the } the  the @end +  -  the #pragma-Macro extraction of single-instance modeBayi //Create a new header file (Singleton.h) the  the //you can use Interfacesingleton to replace the person class Singleton class factory method declaration - #defineInterfacesingleton (name) + (instancetype) share# #name; -  the //If the current project is under Arc the #if__has_feature (OBJC_ARC) the //ARC the #defineImplementationsingleton (name) -+(instancetype) share# #name the { theName *instance =[[Self alloc] init]; the     returninstance;94 } the StaticName *_instance =Nil; the+ (Instancetype) Allocwithzone: (struct_nszone *) Zone the {98     if(_instance = =Nil) { About_instance =[[Super Allocwithzone:zone] init]; -     }101     return_instance;102 }103- (ID) Copywithzone: (Nszone *) Zone104 { the     return_instance;106 }107- (ID) Mutablecopywithzone: (Nszone *) Zone108 {109     return_instance; the }111 #else the //MRC113 #defineImplementationsingleton (name) the+(instancetype) share# #name the { theName *instance =[[Self alloc] init];117 returninstance;118 }119 StaticName *_instance =Nil; -+ (Instancetype) Allocwithzone: (struct_nszone *) Zone121 {122 if(_instance = =Nil) {123_instance =[[Super Allocwithzone:zone] init];124 } the return_instance;126 }127- (ID) Copywithzone: (Nszone *) Zone - {129 return_instance; the }131- (ID) Mutablecopywithzone: (Nszone *) Zone the {133 return_instance;134 }135-(OneWayvoid) Release136 {137 }138-(instancetype) retain139 { $     return_instance;141 }142-(Nsuinteger) Retaincount143 {144     returnmaxfloat;145 }146 //End Conditional Compilation147 #endif148 149  Max by extracting a singleton pattern-related code into a header file, you can then import the header file into the class and pass in the class name151  the #pragma-Person.h file after a single macro extraction153 154 #import<Foundation/Foundation.h>155 #import "Singleton.h"156 @interfacePerson:nsobject <NSCopying,NSMutableCopying>157 158Interfacesingleton (person)//macro definition, the compiler will replace the macro name with + (instancetype) Shareperson;159 //arguments are passed to tell the macro to replace the time declared by the method share what is followed by the name the 161 @end162 163 164 #pragma-person.m file after a single macro extraction165 166 #import "Person.h"167 168 @implementation Person169  the //through the macro definition, to the implementation of the code in the file alias, in the implementation of directly write the macro name + the current class name is OK171 Implementationsingleton (person)172 @end
View Code

Single case design pattern in OC and macro extraction of single case

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.