The singleton design pattern in IOS

Source: Internet
Author: User

A singleton design pattern: In its core structure, it contains only a special class called a singleton class. For example, Nsuserdefault in file management, uiapplication in an application, the entire application is a singleton class that is responsible for some of the operations of the application, which can be obtained under the single file.

A single example design mode can ensure that a class in the system has only one instance and that the instance is easy to be accessed by the outside world, which facilitates the control of the number of instances and saves system resources. Singleton mode is the best choice if you want to have only one object for a class in the system.

Here's a bit of realism, creating a singleton code

Method 1: Create a single case based on thread safety

. h make a statement

+ (ID) sharedinstancebysynchronized;

. m implements the Singleton class

+ (ID) sharedinstancebysynchronized
{
Static Instance *instance;
Guaranteed Singleton thread-safe creation
@synchronized (self) {
if (Instance==nil) {
Instance=[[self alloc] init];
}

}
return instance;
This allows you to invoke the interface API for a singleton class when you need to import the header file to use Method 2: The GCD is a highly encapsulated thread, and the thread is further detailed later. h + (ID) sharedinstancebygcd; m//using GCD handwriting single case
+ (ID) sharedinstancebygcd
{
Static Instance *instance;

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{
Instance=[[self alloc] init];
});
return instance;
} test is not the correct generation of a singleton, we need to test ourselves with the code, by printing out the address of two variables, we know whether the same singleton class Instance *instance1=[instance SHAREDINSTANCEBYGCD];

Instance *instance2=[instance SHAREDINSTANCEBYGCD];

NSLog (@ "%@---%@", instance1,instance2);
The address of the PrintOut Instance1=instance2, stating that a single instance was created through the thread lock
Instance *instance3=[instance sharedinstancebysynchronized];


Instance *instance4=[instance sharedinstancebysynchronized];

NSLog (@ "%@---%@", instance3,instance4); The address of the PrintOut Instance3=instance4, stating that the GCD was created successfully by a single example 2015-11-01 22:38:04.700 single test performance [ 746:28450] 11<instance:0x7f99c35170c0>---<Instance:0x7f99c35170c0>
2015-11-01 22:38:04.700 Single-instance test performance [746:28450] 11<instance:0x7f99c3630d30>---<Instance:0x7f99c3630d30> Two singleton addresses are the same, create a single case success here we do the expansion, test the performance code of two single examples are as follows: need to use a single case to import a single case header file can be called in this file [self test1],[self test2], look at the time difference, you can know the performance of good or bad-( void) Test1
{
Long num=1000;
Cfabsolutetime start=cfabsolutetimegetcurrent ();
for (int i=0; i<num; i++) {

Instance *inst=[instance SHAREDINSTANCEBYGCD];
}
Cfabsolutetime end=cfabsolutetimegetcurrent ();

NSLog (@ "test1:%f", End-start);

}
-(void) test2
{
Long num=1000;
Cfabsolutetime start=cfabsolutetimegetcurrent ();
for (int i=0; i<num; i++) {

Instance *ins=[instance sharedinstancebysynchronized];
}
Cfabsolutetime end=cfabsolutetimegetcurrent ();

NSLog (@ "test2:%f", End-start);


}
2015-11-01 22:38:05.880 Single-instance test performance [746:28450] test1:0.000168
2015-11-01 22:38:05.880 Single-instance test performance [746:28450] test2:0.000319
2015-11-01 22:38:08.128 Single-instance test performance [746:28450] test1:0.000164
2015-11-01 22:38:08.128 Single-case test performance [746:28450] test2:0.000327 from the printed figure can be seen gcd single case better than synchronized performance

The singleton design pattern in IOS

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.