" Singleton mode " is one of the most common design patterns I use in iOS. The singleton mode does not need to pass any parameters, it effectively solves the problem of data sharing between different code. Singleton classes are a very important concept because they exhibit a very useful design pattern. The Singleton class is used throughout the iOS SDK. For example, the UIApplication class has a method called Sharedapplication, which calls this method from anywhere and returns the UIApplication instance associated with the currently running application. In addition to this, Nsnotificationcenter (Message Center), Nsfilemanager (file management), Nsuserdefaults (Application settings), Nsurlcache (Request cache), The Nshttpcookiestorage (application cookie Pool) is a system singleton, and the Singleton class guarantees that there is only one instance object of that class in the application's life cycle and is easily accessible to the outside world.
The main points of the singleton pattern:
One is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.
Advantages of Singleton mode:
1. Instance control: Singleton prevents other objects from instantiating copies of their own Singleton objects, ensuring that all objects have access to unique instances. 2. Flexibility: Because classes control the instantiation process, classes can be more flexible in modifying the instantiation processSingleton mode in iOS:To implement a singleton class in objective-c, you need to do at least the following four steps:
1, for a singleton object to implement a static instance, and initialize, and then set to nil,
2. Implement an instance construction method to check if the static instance declared above is nil, and if it is new and returns an instance of this class,
3, rewrite the Allocwithzone method, to ensure that other people directly use Alloc and Init to try to gain a new strength when the time does not produce a new instance,
4, the appropriate implementation of Copywithzone,release and Autorelease.the implementation of the Singleton pattern:
code example:
MySingletonClass.h
#import <Foundation/Foundation.h> @interface mysingletonclass:nsobject//Single-case method
+ (Mysingletonclass *) sharedinstance; @end
Mysingletonclass.m
#import "MySingletonClass.h" @implementation mysingletonclass//1, implement a static instance for the Singleton object, initialize it, and set it to nil,static Mysingletonclass *manager = NIL;//2, implements an instance construction method to check if the static instance declared above is nil, and if it is new and returns an instance of this class, + (Mysingletonclass *) sharedinstance{@synchronized (self) {if (manager = = nil) {manager = [[Mysingletonclass alloc] I NIT]; Assignment not do here}} return manager;} 3, rewrite the Allocwithzone method, to ensure that other people directly use Alloc and Init to try to acquire a new strength when not to produce a new instance, + (ID) Allocwithzone: (Nszone *) zone{@synchronized (self) {if (!manager) {manager = [super Allocwithzone:zone];//Make sure the same memory address is used Return manager; } return nil; }}//4, the proper realization of copywithzone,release and autorelease. -(ID) init; {@synchronized (self) {if (self = [super init]) {return self; } return nil; }}//ensure that the Copy object is also unique-(ID) Copywithzone: (Nszone *) zone; {return self; }
This article sample source code download: http://www.oschina.net/code/snippet_2337537_52413
Reference Links:
Http://www.cnblogs.com/lyanet/archive/2013/01/11/2856468.html
Http://blog.sina.com.cn/s/blog_71715bf80101a8mm.html
IOS Singleton mode