单例模式
单例模式记录~~~
单例模式提供了一种创建对象的最佳方式。这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。
单例模式的作用
保证在程序运行过程中,一个类只有一个实例,该实例易于供外界访问,从而方便地控制了实例个数,节约系统资源
单例模式的使用场合
在整个应用程序中,需要共享一份资源(资源只需要初始化一次)
单例模式的实现
- ARC环境下实现单例模式
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53方法:懒加载,类方法,协议实现
//ToolBox.m
#import "ToolBox.h"
@interface ToolBox() <NSCopying, NSMutableCopying>
@end
@implementation ToolBox
//0.提供全局变量
static ToolBox *toolBox;
//懒加载 --- 保证只分配一次存储空间
//alloc -> allocWithZone
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
//线程安全问题
//1.互斥锁
// @synchronized (self) {
// if(toolBox == nil) {
// toolBox = [super allocWithZone:zone];
// }
// }
//2.GCD一次性代码 -- 程序运行期间只执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
toolBox = [super allocWithZone:zone];
});
return toolBox;
}
//类方法
//1.方便访问 -- 外界只需调用类方法获得该类实例
//2.表明身份 -- 从类方法识别该类是单例
//3.命名规范 -- share+类名|default+类名|share|default|类名
+(instancetype)shareTool {
return [[self alloc] init];
}
//协议实现
//copy -> copyWithZone
- (id)copyWithZone:(NSZone *)zone {
return toolBox;
}
//mutableCopy -> mutableCopyWithZone
- (id)mutableCopyWithZone:(NSZone *)zone {
return toolBox;
}
@end
1 | //ViewController.m |