Implementation of IOS Singleton Mode

Source: Internet
Author: User

The Singleton Mode means that there is only one instance. The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. This class is called a singleton class.

1. Highlights of Singleton mode:

Obviously, the singleton mode has three key points. One is that a class can only have one instance; the other is that it must create the instance on its own; and the third is that it must provide the instance to the entire system on its own.

2. Advantages of Singleton mode:

1. instance control: Singleton will prevent other objects from instantiating copies of its own singleton object, so that all objects can access a unique instance. 2. flexibility: Because classes control the instantiation process, classes can be more flexible to modify the singleton mode in IOS. In objective-C, a singleton class must be implemented, at least four steps are required:

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.

A singleton instance refers to an instance with static allocation, and all instances in the iPhone SDK, such

[Uiapplication sharedapplication] returns a pointer to a singleton object that represents the application. [Uidevice currentdevice] obtains an object that represents all hardware platforms.

By combining the class method with the Singleton, you can access the static instance anywhere in the program without using a pointer to the object or saving its instance variables. Function example for creating a unique instance (Common Singleton) of a class:

// In many cases, we use a unique instance of a class. The most common is the main class of a program. The following is a singleton function created with the name rootviewcontroller:

Static rootviewcontroller * sharedrootcontroller = nil;

+ (Rootviewcontroller *) sharedcontroller {
 
@ Synchronized (Self)
{
If (sharedrootcontroller = nil)
{
Sharedrootcontroller = [[self alloc] init] autorelease];
}
}
Return sharedrootcontroller;
}

+ (ID) allocwithzone :( nszone *) Zone
{
@ Synchronized (Self)
{
If (sharedrootcontroller = nil)
{
Sharedrootcontroller = [Super allocwithzone: Zone];
Return sharedrootcontroller;
}
}
Return nil;
}

Code Description:

1. Synchronized is mainly a multi-threaded program. This command can limit the code in {} to one thread for execution. If a thread is not finished, other threads have to wait if they need to be executed.

2. The online search code seems to have been not added to autorelease. I think it should be added. It is troublesome if the called function does not have release (I think autorelease should be considered for programs on iOS used to create function return values ).

3. allocwithzone is overloaded because it reads information from the specified memory area to create an instance. Therefore, if the required Singleton already exists, you must disable the modification of the current Singleton, therefore, Nil is returned.

4. nszone: you can think of it as a memory pool. alloc or dealloc operations are performed in this memory pool, and cocoa always allocates a default nszone, any Default memory operation is performed on this zone, and the use of the default zone has a defect because it is global and frequent use will cause memory fragmentation, especially when there are a large number of alloc and dealloc, the performance will be affected. Because you can generate a zone by yourself and restrict alloc and copy to this zone.

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.