Advantages of Singleton mode:
- 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.
- 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.
- 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.
- 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:
- 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.
- 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.
- 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:
- An environment that requires a unique serial number to be generated;
- 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;
- Creating an object consumes too much resources, such as accessing resources such as IO and databases;
- 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