The singleton design pattern in iOS

Source: Internet
Author: User

The singleton design pattern is one of the most important and common design patterns in iOS development. It is designed to always return an instance, no matter how many times it is requested, that is, there is only one instance of a class. Here is an image of the Apple Official document on the singleton mode:

, the diagram on the left is the default multi-mode, so long as the request is sent to create the object, a new object is obtained, while the diagram on the right is a singleton pattern, sending multiple requests to create the object, but the last return is always the same.

Because an instance of a singleton class is created when it is ensured that there are no other instances, and is always the same instance in the process used in the program, the Singleton class can serve as a class that provides a global access to resources, such as nsuserdefaults, which we can store data from. The data inside it is global, regardless of the class in which the operation is the same. In addition, a singleton class can prevent callers from copying, preserving, or releasing instances. Therefore, you can create different singleton classes as needed in the project you are developing.

The singleton creation of NON-ARC (non-ARC) and ARC+GCD, which is now widely used, guarantees thread safety, satisfies the requirements of the static analyzer, and is compatible with ARC, the code is as follows:

+ (Accountmanager *) sharedmanager{    static Accountmanager *sharedaccountmanagerinstance = nil;    static dispatch_once_t predicate;    Dispatch_once (&predicate, ^{        sharedaccountmanagerinstance = [[Self alloc] init];    });    return sharedaccountmanagerinstance;}

  Code Analysis:

With this class method, you can get a singleton object for the current class.

An instance is declared in the method and is initialized to nil, and the preceding static keyword guarantees that only one operation of nil will be performed. The dispatch_once_t is in multi-threading and is guaranteed to execute only once. Dispatch_once This function is used to check if the code block has been called, which not only ensures that the code initialized within the block is run only once, but also ensures thread security.

The use of a singleton is very simple, you can create any of the singleton classes you want by using the class method above, just change the Accountmanager to the name of the class you want to create, and then create the instance with the following code:

Accountmanager *manager = [Accountmanager Sharedmanager];

The singleton design pattern in iOS

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.