Objective-C Singleton)

Source: Internet
Author: User

Singleton is an important concept. It is an extremely convenient design model. A large number of Singleton concepts are used in the iPhone SDK. For example, the sharedapplication method of uiapplication returns a uiapplication instance of the current application at any time.

 

Sometimes we need to use a certain data in almost all parts of a program and perform operations on it. This data is single, and only one data is needed globally, in this case, we may use the Singleton.

Although the Singleton is easy to use, do not abuse it, because the singleton program is always running and won't be destroyed (it is equivalent to directly backing up the memory)

In the previous airline project of the company, the date used by the user for booking tickets, and other related information should be used as a singleton. The shopping cart in the e-commerce App should also be used as a singleton.

 

Next let's talk about how to create a single example ============ because there are many old projects that are MRC, we will also use the singleton. Here we will also provide the method for creating the MRC order example.

1. Implement a static instance for the singleton object, initialize it, and set it to nil,

2. Implement an instance constructor to check whether the static Instance declared above is nil. If yes, create a new instance and return an instance of this class,

3. Rewrite the allocwithzone method to ensure that other people will not generate a new instance when using alloc and init to obtain a new strength,

4. Implement allocwithezone, copywithzone, release, and autorelease as appropriate (the specific methods in MRC such as release are not required under Arc)

 

When there are multiple Singleton classes to be implemented in our project, the names of the classes are different and the other classes are basically the same. Therefore, we 'd better extract the singleton into a macro.

The macro is shown below.

//. H file implementation # define singletonh (methodname) + (instancetype) shared # methodname ;//. implementation of M file # If _ has_feature (objc_arc) // is arc # define singletonm (methodname) Static ID _ instace = nil; + (ID) allocwithzone :( struct _ nszone *) zone {If (_ instace = nil) {static dispatch_once_t oncetoken; dispatch_once (& oncetoken, ^ {_ instace = [Super allocwithzone: Zone] ;});} return _ instace;}-(ID) Init {static dispatch_once_t oncetoken; dispatch_once (& oncetoken, ^ {_ instace = [Super init] ;}); return _ instace ;} + (instancetype) shared # methodname {return [[self alloc] init];} + (ID) copywithzone :( struct _ nszone *) Zone {return _ instace ;} + (ID) mutablecopywithzone :( struct _ nszone *) Zone {return _ instace;} # else // not arc # define singletonm (methodname) Static ID _ instace = nil; + (ID) allocwithzone :( struct _ nszone *) Zone {If (_ instace = nil) {static dispatch_once_t oncetoken; dispatch_once (& oncetoken, ^ {_ instace = [Super allocwithzone: zone] ;}) ;}return _ instace ;}- (ID) Init {static dispatch_once_t oncetoken; dispatch_once (& oncetoken, ^ {_ instace = [Super init] ;}); return _ instace ;}+ (instancetype) shared ## methodname {return [[self alloc] init] ;}- (oneway void) Release {}- (ID) retain {return self;}-(nsuinteger) retaincount {return 1 ;}+ (ID) copywithzone :( struct _ nszone *) Zone {return _ instace ;}+ (ID) mutablecopywithzone :( struct _ nszone *) Zone {return _ instace;} # endif

 

Use has_feature to determine whether or not to use arc. When this macro is introduced, you don't have to worry about whether it is arc or not.

You only need to write in the. h file that implements the Singleton.

Singletonh (methodname)

Write in The. M file

Singletonm (methodname)

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.