Cocoa cola-Cocoa Framework Singleton Mode

Source: Internet
Author: User

Cocoa cola-Cocoa Framework Singleton Mode

Singleton (Singleton mode) is a common software design mode. When this mode is applied, the class of the singleton object must exist with only one instance. In many cases, the whole system only needs to have one global object, which is helpful for us to coordinate the overall behavior of the system. For example, in a server program, the configuration information of the modified server is stored in a file, and the configuration data is read by a single instance object in a unified manner, other objects in the service process then obtain the configuration information through the singleton object. This method simplifies Configuration Management in a complex environment.

The idea of implementing the singleton mode is that a class can return an instance of the object (always the same) and a method to obtain the instance (must be a static method, the getInstance name is usually used). When we call this method, if the Instance held by the class is not empty, this instance is returned. If the instance maintained by the class is empty, create an instance of this class and assign the instance to the instance of this class, so that you can obtain the unique instance of this class only through the static method provided by this class.

The Singleton mode must be used with caution when multiple threads are used. When the unique instance has not been created, if two threads call the creation method at the same time, they do not detect the existence of the unique instance, and thus create an instance at the same time, in this way, the two instances are constructed, which violates the unique principle of instances in the singleton mode. The solution to this problem is to provide a mutex lock (although this will reduce the efficiency) for the variables that have been instantiated for the tag class ).

To create a singleton method in Objective-C, follow these steps.

Step 1 declare a static instance for your Singleton class and initialize it with the value nil. Step 2: In the instance obtaining method (for example, getClassA In the example), a class instance is generated only when the static instance is nil. This instance is usually called a shared instance. Step 3 rewrite the allocWithZone method for determination: you cannot use other methods to create an instance of our class, so that you can only obtain the instance of this class by obtaining the instance method. Therefore, the shared class instance is directly returned in the allocWithZone method. Step 4: implement the basic protocol Methods copyWithZone, release, retain, retainCount, and autorelease to ensure that a single instance has a correct state. The last four methods are used for Memory Management Code and are not suitable for garbage collection code.

The sample code is as follows:

@implementation Singletonstatic Singleton *singleton=nil;+(Singleton*) getInstance{    if (singleton==nil) {        singleton=[[super allocWithZone:NULL]init];    }    return singleton;}@end

 

 

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.