单例模式在iOS开发中可能算是最经常使用的模式之一了,可是因为oc自己的语言特性,想要写一个正确的单例模式相对来讲比较麻烦,这里我就抛砖引玉来聊一聊iOS中单例模式的设计思路。关于单例模式更多的介绍请参考这篇文章。html
单例顾名思义就是说一个类的实例只能有一个,在java、C++这类语言中,能够经过将构造函数私有化来避免对象的重复建立,可是objective-c却不可以这样作,咱们须要经过其余机制来达到这个目的。通常状况下,可能咱们写的单例模式是这样的:java
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
|
#
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
</foundation>
|
具体使用:objective-c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#
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
;
}</foundation>
|
输出结果为 :函数
1
2
3
|
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
=
""
>.</singleton:></singleton:></singleton:>
|
能够看到,当咱们调用shareInstance方法时获取到的对象是相同的,可是当咱们经过alloc和init来构造对象的时候,获得的对象倒是不同的。spa
那么问题就来了,咱们经过不一样的途径获得不一样的对象,显然是不行的。咱们必需要确保对象的惟一性,因此咱们就须要封锁用户经过alloc和init以及copy来构造对象这条道路。设计
咱们知道,建立对象的步骤分为申请内存(alloc)、初始化(init)这两个步骤,咱们要确保对象的惟一性,所以在第一步这个阶段咱们就要拦截它。当 咱们调用alloc方法时,oc内部会调用allocWithZone这个方法来申请内存,咱们覆写这个方法,而后在这个方法中调用 shareInstance方法返回单例对象,这样就能够达到咱们的目的。拷贝对象也是一样的原理,覆写copyWithZone方法,而后在这个方法中 调用shareInstance方法返回单例对象。看代码吧:code
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
|
#
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
|
再看看效果如何:htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
;
}</foundation>
|
输出结果:对象
1
2
3
4
|
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
=
""
>.</singleton:></singleton:></singleton:></singleton:>
|
能够看到获取到的对象都是同样的了。内存