Singleton implementation (Singleton macro), implementation (macro)
1. What is a Singleton?
A singleton is a class. Each creation is the same object. That is to say, it can only be instantiated once.
2. How to ensure that each creation is the same object
Creating an object will eventually go through a path where the alloc method (the alloc method calls allocWithZone :). Therefore, you only need to ensure that the alloc method is called once and thread security is ensured, and then put this object in the static zone. In the future, no matter whether the object is created or the copy object, the objects in the static zone will be directly returned.
Iii. Notes
Static global variables do not need to be released (applicable to MRC). To solve thread security problems, you can use mutex lock or GCD, which is better.
You can also disable repeated object initialization, that is, the initialization method can be executed only once.
4. The specific implementation code is as follows:
@ Implementation myManagerstatic id instance; + (instancetype) allocWithZone :( struct _ NSZone *) zone {// 1. mutex lock // @ synchronized (self) {// if (instance = nil) // {// instance = [super allocWithZone: zone]; //} // 2. GCD, execute static dispatch_once_t onceToken only once; dispatch_once (& onceToken, ^ {if (instance = nil) {instance = [super allocWithZone: zone] ;}}); return instance ;} + (instancetype) sharedSoundTools {instance = [[self alloc] init]; return instance;}-(id) copyWithZone :( NSZone *) zone {return instance ;} # pragma mark-MRC part of the code-(oneway void) release {// do nothing}-(instancetype) retain {// never do anything, however, it needs to return the return instance;}-(instancetype) autorelease {return instance;}-(NSUInteger) retainCount {// This prevents the brute force release objects in unknown records, for example, while loop return ULONG_MAX;}-(instancetype) init {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {self = [super init]; if (self) {self. age = 10; // For example, only initialize once. You can leave this parameter Unspecified.}); return self ;}@ end
V. Singleton macro
The following code is written in a singleton. h file, which contains the. h header file.
Usage method,. h file: singletonInterface (myManager );
. M file: singletonImplementation (myManager );
The extraction code is as follows:
# Define singletonInterface (className) + (instancetype) shared # className; # if _ has_feature (objc_arc) // below is the ARC version # define singletonImplementation (className) \ + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ if (instance = nil) {\ instance = [super allocWithZone: zone] ;\}\}); \ return instance ;\}\+ (instancetype) shared ## className {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ instance = [[self alloc] init] ;\}); \ return instance ;\}\-(id) copyWithZone :( NSZone *) zone {\ return instance; \}# else // The MRC version # define singletonImplementation (className) \ + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ if (instance = nil) {\ instance = [super allocWithZone: zone] ;\}\}); \ return instance; \}\+ (instancetype) shared # className {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ instance = [self alloc] init] ;\}); \ return instance ;\}\-(id) copyWithZone :( NSZone *) zone {\ return instance ;\}\-(oneway void) release {}\-(instancetype) retain {return instance ;}\-(instancetype) autorelease {return instance ;}\-(NSUInteger) retainCount {return ULONG_MAX ;}# endif // you are prompted not to have \