Original address: http://www.galloway.me.uk/tutorials/singleton-classes/
This article for Bo Master self-translation, reproduced please indicate the source: http://blog.csdn.net/iosevanhuang/article/details/13278323
"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.
Background
Singleton classes are a very important concept because they exhibit a very useful design pattern. The application of the Singleton class is used throughout the iphone 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.
Blogger Supplement: 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.
Realize
You can implement a Objective-c singleton class with the following code (in Arc mode, not in arc mode):
1 2 3 4 5 6 7 8 9 |
#import <foundation/foundation.h> @interface mymanager: nsobject { NSString *someproperty; } @property (nonatomic retain) nsstring * someproperty + (idsharedmanager< Span class= "P"; @end |
code slices from codesMyManager.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#import "MyManager.h"@implementation Mymanager @synthesize someproperty; #pragma mark Singleton Methods+ (ID)sharedmanager { static mymanager *sharedmymanager = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ Sharedmymanager = [[self alloc] init]; });return Sharedmymanager; } - (ID)init { if (self = [super Init]) { someproperty = [[nsstring alloc] initwithstring:@ "Default property Value "]; }return self; } - (void)dealloc { //Never call this method}@end |
code slices from codesMymanager.m
Here we define a static variable Sharedmymanager, and then initialize the variable only once. Dispatch_once is used here to ensure that the variable is initialized only once by GCD. The operating system will ensure that this is thread-safe.
If you do not use GCD, you can use the following code:
1 2 3 4 5 6 7 8 |
+ (ID)sharedmanager { static mymanager *sharedmymanager = nil; @synchronized(self) { if (sharedmymanager = = nil) Sharedmymanager = [[self alloc] init]; }return Sharedmymanager; } |
code slices from codesNON-GCD-BASED-CODE.M Next, when you need to use this singleton object, you can just call it:
1 |
Mymanager *sharedmanager = [mymanager Sharedmanager]; |
code slices from codesSNIPPET_FILE_0.M I use singleton classes extensively in my code, such as creating a singleton class to handle corelocation or CoreData functions.
Non-ARC codeAlthough I don't recommend it, you should use the following code if you want to use a non-ARC environment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 1 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#import "MyManager.h" static mymanager *sharedmymanager = nil; @implementation Mymanager @synthesize someproperty; #pragma mark Singleton Methods+ (ID)sharedmanager { @synchronized(self) { if(sharedmymanager = = nil) Sharedmymanager = [[Super Allocwithzone:NULL] init]; }return Sharedmymanager; }+ (ID)allocwithzone:(nszone *)zone { return [[self sharedmanager] retain]; }- (ID)copywithzone:(nszone *)zone { return self; }- (ID)retain { return self; }- (unsigned)retaincount { return Uint_max; //Indicates that the object should never be released }- (oneway void)release { //Never release}- (ID)autorelease { return self; }- (ID)init { if (self = [super Init]) { someproperty = [[nsstring alloc] initwithstring:@ "Default property Value "]; }return self; }- (void)dealloc { //Never call this method[someproperty release]; [Super Dealloc]; }@end |
code slices from codesMYMANAGER-NON-ARC.M: The singleton here is a lazy single case, when the acquisition method of a singleton object is first called to create a singleton object. When a class is loaded, a singleton object is created, called a A hungry man-type singleton.
iOS single-case mode