Objective-C,iOS严格单例模式正确实现

注:本文所有权归作者所有,转载请注明出处  

  当希望在一个应用程序中某个类的对象只能存在一个的时候就可以考虑用单例模式来实现,单例模式在C++中比较容易实现(只需把构造函数声明为private),而在Objective-C中对象可以通过NSObject的alloc来产生,所以需要编写一些额外的代码来确保对象的唯一性,考虑到现在编写iOS APP代码几乎都是ARC方式,且GCD也已经被用烂了,故本文给出一种利用GCD技术来实现严格单例模式的ARC版本,具体代码如下所示,所有的注意点都写在了注释里面:

 1 Singleton.h
 2 @interface Singleton : NSObject
 3 @property(nonatomic,strong) NSString *name;
 4 +(Singleton*)defaultManager;
 5 @end
 6 
 7 
 8 Singleton.m
 9 @implementation Singleton
10 //单例类的静态实例对象,因对象需要唯一性,故只能是static类型
11 static Singleton *defaultManager = nil;
12 
13 /**单例模式对外的唯一接口,用到的dispatch_once函数在一个应用程序内只会执行一次,且dispatch_once能确保线程安全
14 */
15 +(Singleton*)defaultManager
16 {
17     static dispatch_once_t token;
18     dispatch_once(&token, ^{
19         if(defaultManager == nil)
20         {
21             defaultManager = [[self alloc] init];
22         }
23     });
24     return defaultManager;
25 }
26 
27 /**覆盖该方法主要确保当用户通过[[Singleton alloc] init]创建对象时对象的唯一性,alloc方法会调用该方法,只不过zone参数默认为nil,因该类覆盖了allocWithZone方法,所以只能通过其父类分配内存,即[super allocWithZone:zone]
28  */
29 +(id)allocWithZone:(struct _NSZone *)zone
30 {
31    static dispatch_once_t token;
32     dispatch_once(&token, ^{
33         if(defaultManager == nil)
34         {
35             defaultManager = [super allocWithZone:zone];
36         }
37     });
38     return defaultManager;
39 }
40 //自定义初始化方法,本例中只有name这一属性
41 - (instancetype)init
42 {
43     self = [super init];
44     if(self)
45     {
46         self.name = @"Singleton";
47     }
48     return self;
49 }
50 
51 //覆盖该方法主要确保当用户通过copy方法产生对象时对象的唯一性
52 - (id)copy
53 {
54     return self;
55 }
56 
57 //覆盖该方法主要确保当用户通过mutableCopy方法产生对象时对象的唯一性
58 - (id)mutableCopy
59 {
60     return self;
61 }
62 //自定义描述信息,用于log详细打印
63 - (NSString *)description
64 {
65     return [NSString stringWithFormat:@"memeory address:%p,property name:%@",self,self.name];
66 }

测试代码如下:

 1 Singleton *defaultManagerSingleton =[Singleton defaultManager];
 2 NSLog(@"defaultManagerSingleton:\n%@",defaultManagerSingleton);
 3 Singleton *allocSingleton = [[Singleton alloc] init];
 4 NSLog(@"allocSingleton:\n%@",allocSingleton);
 5 Singleton *copySingleton = [allocSingleton copy];
 6 NSLog(@"copySingleton:\n%@",copySingleton);
 7 Singleton *mutebleCopySingleton = [allocSingleton mutableCopy];
 8 NSLog(@"mutebleCopySingleton:\n%@",mutebleCopySingleton);
 9 
10 //打印结果
11 2015-10-11 21:48:34.722 Singleton[1941:214584] defaultManagerSingleton:
12 memeory address:0x7fa6d1591530,property name:Singleton
13 2015-10-11 21:48:34.727 Singleton[1941:214584] allocSingleton:
14 memeory address:0x7fa6d1591530,property name:Singleton
15 2015-10-11 21:48:34.727 Singleton[1941:214584] copySingleton:
16 memeory address:0x7fa6d1591530,property name:Singleton
17 2015-10-11 21:48:34.727 Singleton[1941:214584] mutebleCopySingleton:
18 memeory address:0x7fa6d1591530,property name:Singleton

从打印结果来看通过 [Singleton defaultManager]、[[Singleton alloc] init]、[allocSingleton copy]、[allocSingleton mutableCopy]这四种方法生成的对象地址都是0x7fa6d1591530,即表明对象是同一个,也就实现了严格单例模式,加上GCD是线程安全的所以在多线程中也能保证对象的唯一性。

另:在学习Objective-C编写单例模式时看到网上好多人都借用苹果官方的实现方式,但我自己始终没搜到官方的实现Sample代码,如果你知道麻烦把网址给我发下,谢谢哈~