Singleton mode in iOS

Source: Internet
Author: User

Advantages of Singleton mode:

    1. Because Singleton mode has only one instance in memory and reduces memory overhead, especially when an object needs to be created, destroyed frequently, and performance is not optimized when it is created or destroyed, the advantages of the singleton pattern are obvious.
    2. Because the singleton pattern generates only one instance, it reduces the performance overhead of the system, and when an object is produced that requires more resources, such as reading the configuration and generating other dependent objects, it can be resolved by directly producing a singleton object when the app starts, and then using a permanent memory-resident approach.
    3. Singleton mode avoids multiple uses of resources, such as a write file action, because only one instance exists in memory and avoids simultaneous write operations on the same resource file.
    4. Singleton mode can be used to set global access points, optimize and share resource access, for example, you can design a singleton class, responsible for all data table mapping processing.

Disadvantages of the Singleton pattern:

    1. The singleton mode generally has no interface, the extension is very difficult, to expand, in addition to modify the code basically there is no second way can be achieved.
    2. The singleton mode is detrimental to the test. In a parallel development environment, if the singleton mode is not completed, it cannot be tested, and no interface can be used to dummy an object in a mock way.
    3. The singleton pattern conflicts with the single duty principle. A class should only implement a single logic, not whether it is a singleton, is not a single case depending on the environment, the singleton mode of "want to Singleton" and business logic in a class.

Usage scenarios for a singleton pattern:

    1. An environment that requires a unique serial number to be generated;
    2. A shared access point or shared data, such as a counter on a Web page, can be used throughout the project to keep the value of the counter and ensure thread safety without having to log each refresh to the database, using singleton mode;
    3. Creating an object consumes too much resources, such as accessing resources such as IO and databases;
    4. You need to define a large number of static and static methods (such as tool classes) of the environment, you can use the Singleton mode (of course, can also be directly declared as static way).

Creating a singleton class

#import <Foundation/Foundation.h>

@interface Contacthelper:nsobject

Create a single-instance method

+ (Contacthelper *) Sharecontacthelper;

@end

#import "ContactHelper.h"

@implementation Contacthelper

+ (Contacthelper *) sharecontacthelper{

static Contacthelper *helper = nil;

Handle multi-threaded data security, plus @synchronized can guarantee this. method can only be accessed by one thread at a time

@synchronized (self) {

if (helper = = nil) {

Helper = [[Contacthelper alloc]init];

}

}

/* Most secure notation under multithreading

static Contacthelper *helper = nil;

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

if (helper = = nil) {

Helper = [[Contacthelper alloc] init];

}

});

*/

return helper;

}

Singleton mode 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.