文章目录
创建型模式
单例模式
系统生命周期内全局只有一个实例的设计模式。
优点
- 避免频繁创建对象
- 节约系统资源
缺点
- 不好扩展
- 职责过重
实现
static BKSingletonBase *singleton = nil;
@implementation BKSingletonBase
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken = 0; //默认为0
dispatch_once(&onceToken, ^{ // onceToken为256
singleton = [[self alloc] init];
}); // onceToken为-1
return singleton;
}
@end