Singleton mode iOS development may be used in one of the most commonly used patterns. But because OC language features itself, want to write a correct singleton mode is more cumbersome, iOS singleton mode design ideas.
Please refer to this article for a number of other introductions to the singleton pattern.
A single example of a class can only have one instance. In languages such as Java and C + +, it is possible to avoid repeated creation of objects by privatizing the constructors. But Objective-c is not able to do so, and we need to do this through other mechanisms. Under normal circumstances, we may write a singleton pattern that is this:
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
+(instancetype) shareInstance ;
@end
#import "Singleton.h"
@implementation Singleton
static Singleton* _instance = nil;
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init] ;
}) ;
return _instance ;
}
@end
Detailed use:
#import <Foundation/Foundation.h>
#import "Singleton.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Singleton* obj1 = [Singleton shareInstance] ;
NSLog(@"obj1 = %@.", obj1) ;
Singleton* obj2 = [Singleton shareInstance] ;
NSLog(@"obj2 = %@.", obj2) ;
//
Singleton* obj3 = [[Singleton alloc] init] ;
NSLog(@"obj3 = %@.", obj3) ;
}
return 0;
}
The output is:
2014-12-15 16:06:28.344 ObjcSingleton[8847:303] obj1 = <Singleton: 0x1001086e0>.
2014-12-15 16:06:28.346 ObjcSingleton[8847:303] obj2 = <Singleton: 0x1001086e0>.
2014-12-15 16:06:28.346 ObjcSingleton[8847:303] obj3 = <Singleton: 0x100103940>.
Can see. The objects we get when we call the Shareinstance method are the same, but when we construct the object through Alloc and init. The objects you get are not the same.
Then the problem comes. We get different objects in different ways, obviously not. We have to ensure the uniqueness of the object, so we need to block the way that the user constructs the object through Alloc and init and copy.
We know that the steps to create the object are divided into the application memory (ALLOC), initialization (init) steps, we want to ensure the uniqueness of the object, so at this stage of the first step we have to intercept it.
When we call the Alloc method, Allocwithzone is called internally by OC to request memory. We override this method and then call the Shareinstance method in this method to return the singleton object, so that we can achieve our goal. Copying an object is also the same principle, overwrite the Copywithzone method, and then call the Shareinstance method in this method to return the singleton object. Look at the code:
#import "Singleton.h"
@implementation Singleton
static Singleton* _instance = nil;
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
}) ;
return _instance ;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
return [Singleton shareInstance] ;
}
-(id) copyWithZone:(struct _NSZone *)zone
{
return [Singleton shareInstance] ;
}
@end
and see how it works:
main :
#import <Foundation/Foundation.h>
#import "Singleton.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Singleton* obj1 = [Singleton shareInstance] ;
NSLog(@"obj1 = %@.", obj1) ;
Singleton* obj2 = [Singleton shareInstance] ;
NSLog(@"obj2 = %@.", obj2) ;
//
Singleton* obj3 = [[Singleton alloc] init] ;
NSLog(@"obj3 = %@.", obj3) ;
Singleton* obj4 = [[Singleton alloc] init] ;
NSLog(@"obj4 = %@.", [obj4 copy]) ;
}
return 0;
}
Output Result:
2014-12-15 16:11:24.734 ObjcSingleton[8979:303] obj1 = <Singleton: 0x100108720>.
2014-12-15 16:11:24.735 ObjcSingleton[8979:303] obj2 = <Singleton: 0x100108720>.
2014-12-15 16:11:24.736 ObjcSingleton[8979:303] obj3 = <Singleton: 0x100108720>.
2014-12-15 16:11:24.736 ObjcSingleton[8979:303] obj4 = <Singleton: 0x100108720>.
You can see that the objects you get are the same.
This is the way of thinking. If there are more stringent wording please leave a message, notice. Thank you ~
Copyright notice: This article mr.simple original article, must not be reproduced without consent.
Objective-c the right to be single