iOS - Singleton Pattern

Singleton classes generally obtain this unique object through a class method

 The " Singleton Pattern " is one of the design patterns we use most often in iOS. The singleton pattern does not need to pass any parameters, which effectively solves the problem of data sharing between different codes. Singleton classes are a very important concept because they represent a very useful design pattern. The application of the singleton class runs through the entire iOS SDK. For example, the UIApplication class has a method called sharedApplication, and calling this method from anywhere will return the UIApplication instance associated with the currently running application. In addition to this, NSNotificationCenter (message center), NSFileManager (file management), NSUserDefaults (persistent storage data), NSURLCache (request cache), NSHTTPCookieStorage (application cookies pool) are all system singletons;

The singleton class ensures that there is only one instance object of this class in the life cycle of the application, and it is easy to be accessed by the outside world.

advantage:

1. Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, thus ensuring that all objects have access to a unique instance.

 2. Flexibility: Because the class controls the instantiation process, the class can modify the instantiation process more flexibly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481968&siteId=291194637