Singleton mode-iOS development

Source: Internet
Author: User
Statement

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
ArticleSource: http://blog.csdn.net/iukey

Singleton is a common software design model. This mode ensures that only one instance (object) exists. In many cases, a class must have only one object at a time and can be called globally.

The idea of Singleton mode is that a class can return an instance of the object (always the same) and a method to obtain the instance (it must be a static method, usually using the getinstance name ); when we call this method, if the Instance held by the class is not empty, the instance is returned. If the Instance held by the class is empty, the instance of the class is created, and assign the instance to the instance that maintains the class, thus limiting the user to obtain the unique instance of the class only through the static method provided by the class.

The Singleton mode must be used with caution in multi-thread scenarios. When a unique instance is not created, if two threads call the creation method at the same time, they do not detect the existence of a unique instance and create an instance at the same time, in this way, two instances are created, which violates the unique instance principle 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 ).

Here is an example to illustrate the problem:

1. Declare a static instance for your Singleton class and initialize it as nil.

2. In the method for obtaining an instance (for example, getinstance in the following example), an instance of your class is generated only when the static instance is nil, this instance is usually called a shared instance.

3. Override the allocwithzone method to determine whether to use other methods to create a non-instance, and restrict users to obtain instances of this class only by obtaining the instance. Therefore, the shared class instance is directly returned in the allocwithzone method.

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 described as follows:CodeDoes not apply to garbage collection code.

@ Implementation tcpclientstatic tcpclient * tcpclient = nil; + (tcpclient *) getinstance {If (tcpclient = nil) {tcpclient = [[Super allocwithzone: NULL] init];} return tcpclient;} + (ID) allocwithzone :( nszone *) Zone {return [self getinstance] retain]; // return Singleton}-(ID) copywithzone :( nszone *) zone {return self;}-(ID) Retain {return self;}-(nsuinteger) retaincount {return nsuintegermax;}-(oneway void) release {// oneway is used in Distributed Object APIs. These APIs can be used in different threads or even differentProgram. The oneway keyword is only used in the definition of a message whose return type is void. Because oneway is asynchronous, the message is not expected to be returned immediately. // Do nothing}-(ID) autorelease {return self;} @ 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.