IOS Singleton mode and iOS Mode
"Singleton Mode"Is one of the most commonly used design patterns in iOS. The Singleton mode effectively solves the problem of data sharing between different codes without passing any parameters. Singleton classes are a very important concept because they demonstrate a very useful design pattern. Singleton applications run throughout the iOS SDK. For example, the UIApplication class has a method named sharedApplication. Calling this method from anywhere will return the UIApplication instance associated with the currently running application. In addition, nsicationicationcenter (message center), NSFileManager (File Management), NSUserDefaults (application settings), NSURLCache (request cache), and NSHTTPCookieStorage (Application cookie pool) are all system Singleton instances; the Singleton class ensures that there is only one instance object in the life cycle of the application, and is easy to access.
Highlights of Singleton mode:
First, a class can only have one instance; second, it must create the instance on its own; and third, it must provide the instance to the entire system on its own.
Advantages of Singleton mode:
1. instance control: Singleton will prevent other objects from instantiating copies of its own Singleton object, so that all objects can access a unique instance. 2. Flexibility: Because the class controls the instantiation process, the class can modify the instantiation process more flexibly.
Singleton mode in IOS:To implement a singleton class in objective-c, perform the following four steps:
1. Implement a static instance for the singleton object, initialize it, and set it to nil,
2. Implement an instance constructor to check whether the static Instance declared above is nil. If yes, create a new instance and return an instance of this class,
3. Rewrite the allocWithZone method to ensure that other people will not generate a new instance when using alloc and init to obtain a new strength,
4. Implement copyWithZone, release, and autorelease as appropriate.
Implementation of Singleton mode:
Sample Code:
MySingletonClass. h
# Import <Foundation/Foundation. h> @ interface MySingletonClass: NSObject // Singleton Method
+ (MySingletonClass *) sharedInstance; @ end
MySingletonClass. m
# Import "MySingletonClass. h "@ implementation MySingletonClass // 1. Implement a static instance for the singlementation object, initialize it, and set it to nil, static MySingletonClass * manager = nil; // 2. Implement an instance constructor to check whether the static Instance declared above is nil. If yes, create a new instance and return an instance of this class, + (MySingletonClass *) sharedInstance {@ synchronized (self) {if (manager = nil) {manager = [[MySingletonClass alloc] init]; // assignment not done here} return manager ;} // 3. Rewrite the allocWithZone method to ensure that others directly use alloc and init to try to obtain a new + (Id) allocWithZone :( NSZone *) zone {@ synchronized (self) {if (! Manager) {manager = [super allocWithZone: zone]; // make sure to use the same memory address return manager;} return nil ;}// 4. Implement copyWithZone as appropriate, release and autorelease. -(Id) init; {@ synchronized (self) {if (self = [super init]) {return self ;}return nil ;}} // make sure that the copy object is also unique-(id) copyWithZone :( NSZone *) zone; {return self ;}
Sample source code download: http://www.oschina.net/code/snippet_2337537_52413
Reference link:
Http://www.cnblogs.com/lyanet/archive/2013/01/11/2856468.html
Http://blog.sina.com.cn/s/blog_71715bf80101a8mm.html