Singleton implementation (Singleton macro), implementation (macro)

Source: Internet
Author: User

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 \

 

Related Article

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.