IOS& design patterns – single case

Single case (singleton)

  • The intent is to ensure that a class has only one instance and provides a global access point to access it.
  • When the class is only one instance and the customer can access should be can be extended by subclassing it when the only instance from one access point and as everyone knows, no need to change the code examples of customers should be able to use an extended time
  • Structure IOS& design patterns - single case,
    , 896CA6D0-7B09-435C-991A-6A7870746B5F.png
  • The advantages and disadvantages of controlled access to a single instance reduce the size of the namespace, allowing refinement of operations and representations, allowing variable instances to be more flexible than class operations
  • The OC version of @interface Singleton: / / non thread safe NSObject + (instancetype) sharedInstance @end @implementation Singleton static Singleton*; instance = nil; / / when the program is running to initialize the singleton //+ (void load) {/ / [Singleton sharedInstance]; //} + sharedInstance (instancetype) {if (instance = = Nil) {instance = [[Singleton alloc] init]; return instance};} / / when we call alloc when the callback method (ensure that only), OC does not allow the construction method of privatization + (ID) allocWithZone: (struct * _NSZone) zone {if (instance = = Nil) {instance} = [super allocWithZone:zone]; return instance;} / / @end @implementation Singleton static Singleton* thread safe instance = nil; + (instancetype) {if (instance = = sharedInstance NIL) {/ / lock unlock is need to consume the performance of @synchronized (self) {if (instance = = Nil) {instance = [[Singleton alloc] init];}}}} + return instance; (ID) allocWithZone: (struct * _NSZone) zone (self) {@synchronized {if (instance = = Nil) {instance = [super allocWithZone:zone] return instance;}};} / / @end -GCD @implementation Singleton static Singleton* thread safety + instance = nil; sharedInstance static dispatch_once_t (instancetype) {once; dispatch_once (& once, instance = [[Singleton03 ^{alloc] init]; return instance;}}); / / when we call the alloc callback method (time to ensure that only), OC does not allow the construction method of privatization + (ID) allocWithZo Ne: (struct _NSZone * zone) {static dispatch_once_t once; dispatch_once (& once, instance ^{= [super allocWithZone:zone]; return instance;}}); / / @end Swift version final class Singleton: NSObject non thread safe {private static var = nil class func instance: Singleton? SharedInstance (-> Singleton) {if instance = = nil {instance = Singleton (return instance)}!} / / private override init construction method of Privatization () {}} / / final class Singleton: NSObject thread safe {static var sharedInstance = Singleton (private) override (init) {}}

For example, how to choose thread safety or non thread safety, if the program does not involve multi threading, give priority to non thread security bar, after all, lock unlock or need to consume resources.