Implementation and use of Singleton in iOS

Source: Internet
Author: User

Implementation and use of Singleton in iOS

Singleton is the most common design pattern in our development, as well as in iOS. A singleton ensures that the instance of a class is unique in the program, facilitating sharing of resources and data. The design principle used is a single responsibility principle. Let's take a look at which Singleton mode is used for classes or methods inherent in iOS:

(1) UIAccelerometer class and sharedAccelerometer method. Generally, if the method name contains a word such as shared, it can be considered as an instance variable that can be shared by the entire application. A singleton is generally used.

(2) UIApplication class and sharedApplication method. We generally use this method to create global variables.

(3) NSBundle class and mainBundle method.

(4) NSFileManager class and defaultManager method.

(5) nsicationicationcenter class and defamanager manager method. Nsicationicationcenter also implements the observer mode.

(6) NSUserDefaults class and defaultUser method.

Today, we try to implement a singleton by ourselves:

(1) create a common class named Singleton. in Singleton. h declare a class method, and then use this class method (Note: it must be a class method, rather than an instance method) to create a unique instance of this class:

 

# Import
 
  
@ Class Singleton; @ interface Singleton: NSObject // "+" indicates the class method, which is called by the class + (Singleton *) sharedInstance; @ end
 

(2) The sharedInstance method and other business logic must be implemented in Singleton. m:
# Import "Singleton. h "// use static to declare a static instance of a class; static Singleton * _ sharedInstance = nil; @ implementation Singleton/*** 1. use the class method to generate a unique instance of this class; */+ (Singleton *) sharedInstance {if (! _ SharedInstance) {_ sharedInstance = [[self alloc] init];} return _ sharedInstance;} @ end

Note: You must declare a static variable. In the future, the sharedInstance method will be used to create a unique instance of the class, instead of alloc, init.

(3) Let's use a simple demo to demonstrate the singleton:

# Import "RootVC. h "# import" Singleton. h "@ interface RootVC () @ end @ implementation RootVC-(void) viewDidLoad {[super viewDidLoad]; [self testSigleTon];}-(void) testSigleTon {// The result of a singleton is to call the class method and return only one common object/*** single and single2 are the same object. Because the returned data is a static variable, globally Unique; */Singleton * single = [Singleton sharedInstance]; Singleton * single2 = [Singleton sharedInstance]; if (single = single2) {NSLog (@ "single = single2");} NSLog (@ "single address: % @", single); NSLog (@ "single2 address: % @", single2);} @ end

(4) the output result is as follows:

.

We can see that the memory addresses of the two objects are the same, indicating that the two objects are actually the same object, and the Singleton is implemented. This is the most common and simple implementation method for a single instance. It is often used in projects and is completely correct without multithreading. However, let's think about whether this implementation method is safe in multi-thread development? So how should we implement it. I will introduce it in my blog later.

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.