define

Singleton pattern: Ensure that a class has only one instance and provide a global access point to access it.

This pattern is common in all kinds of projects, where a singleton class provides a point of access for creating a unique object that accesses the class and ensures that it is unique, consistent, and well known.

With other modes

  • The factory method
  • Command mode

Usage scenarios

Consider using the singleton pattern when:

  • Classes can have only one instance and must be accessed from a well-known access point, such as a factory method
  • This unique instance can only be extended by subclassing, and the extended object does not break the client code.

The class diagram structure

The singleton class diagram is as follows:

Code implementation

Let’s implement the singleton method in code

@interface XQSingleton : NSObject

+ (instancetype)shareInstance;

@end

@implementation XQSingleton

+ (instancetype)shareInstance{
    static XQSingleton *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[super allocWithZone:NULL] init];
    });
    return shareInstance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    return [self shareInstance];
}

- (id)copy{
    return self;
}

- (id)mutableCopy{
    return self;
}

@end
Copy the code

The above code has the following points:

  • Declare the shareInstance interface in @Interface of XQSingleton, which is provided to external calls to return singleton objects.
  • In the class implementation, shareInstance uses static XQSingleton *shareInstance, which is a static local object to keep the returned object unique.
  • Using GCD’s dispatch_once function ensures that the initialization method of this object is executed only once, ensuring thread-safety.
  • Initialization uses [super allocWithZone:NULL] to override the allocWithZone method of the current class. Objects can only be created using the parent class’s object allocation method.
  • The allocWithZone method is overridden to prevent external calls to alloc from creating another new XQSingleton object.
  • Overloading copy and mutableCopy methods prevents new objects from being generated when external calls to copy or mutableCopy methods are made.

The Cocoa Touch framework uses singletons

  1. UIApplication classes UIApplication singleton classes are extremely common in frameworks. It provides a central point of control and coordination for iOS applications. Handles many housekeeping tasks in the application, including initial routing of incoming user events, distribution of UIControl events to appropriate target objects, maintaining a list of all UIWindow objects open in the application, and so on.
  2. NSFileManager class NSFileManager creates and accesses file systems using the defaultManager class methods. Provides a unified access interface.

conclusion

Whenever an application needs a centralized class to coordinate its services, that class should generate a single instance, not multiple instances.