Singleton design mode and IOS design mode in IOS

Source: Internet
Author: User

Singleton design mode and IOS design mode in IOS

The Singleton design mode is a very important and common design mode in IOS development. It is designed to always return an instance regardless of the number of requests, that is, a class has only one instance. The following is an image of the singleton mode in Apple's official documentation:

 

The graph on the left is the default multi-sample mode. If you send a request to create an object, a new object will be generated. The graph on the right is the singleton mode, send multiple requests to create an object, but always return the same object.

This is because an instance of the singleton class is created only when no other instance is created, and it is always the same instance when used in the program, therefore, the singleton class can be used as a class to provide global resource access. For example, NSUserDefaults can store data and the data in it is global, the operation is the same in any class. In addition, a singleton class prevents callers from copying, retaining, or releasing instances. Therefore, you can create different Singleton classes as needed in the development project.

The creation of a singleton is divided into Non-ARC (Non-ARC) and ARC + GCD. The latter is widely used. It ensures thread security and meets the requirements of static analyzer, 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:

This class method can be used to obtain the singleton object of the current class.

First declare an instance in the method and initialize it as nil. The previous static keyword can ensure that only one operation is performed as nil. Dispatch_once_t is in multi-thread mode and is only executed once. The dispatch_once function is used to check whether the code block has been called. It not only ensures that the Code initialized in the block is only run once, but also ensures thread security.

The use of Singleton is very simple. You can use the class method above to create any Singleton class you want. You only need to change AccountManager to the name of the class you want to create, then, use the following code to create an instance:

AccountManager *manager = [AccountManager sharedManager];

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.