单例模式(Singleton Pattern):保证一个类只有一个实例,并且提供一个访问它的全局访问点。
单例在程序设计中是最常见的设计模式之一,下面我们来实现一个单例:
Singleton.h 文件
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
+ (instancetype) shareInstance;
@end
Singleton.m 文件
static Singleton * singleton = nil;
@implementation Singleton
+ (instancetype) shareInstance {
return [self allocWithZone:nil];
}
+(id)allocWithZone:(NSZone *)zone{
if (!singleton) {
singleton = [[super alloc] init];
}
return singleton;
}
- (id)copyWithZone:(NSZone *)zone;{
return self ;
}
这样写好像就能够实现单例模式,但是我们要想,它真的能够保证程序中只有一个单例类的对象么?答案显然是否定的。当shareInstance方法第一次调用没有完成的时候,单例对象还是nil,这时候如果另外一个线程在此调用shareInstance方法会发生什么呢?显然,这个时候系统会初始化两次单例对象,也就是说这个时候不是单例了,因为这时候的单例类已经不止一个实例对象了。那么怎么才能保证单例的线程安全呢,这就是线面要实现的:
方法一:加线程锁
对单例对象初始化方法添加线程锁,当其他线程在此访问时线程锁能够保证单例对象在初始化完成之前不能再次调用初始化方法。
#import "Singleton.h"
static Singleton * singleton = nil;
@implementation Singleton
+ (instancetype) shareInstance {
return [self allocWithZone:nil];
}
+(id)allocWithZone:(NSZone *)zone{
@synchronized(self){
if (!singleton) {
singleton = [[Singleton alloc] init];
}
}
return singleton;
}
- (id) copyWithZone:(NSZone *)zone;{
return self;
}
方法二:GCD实现
gcd 中dispatch_once函数在程序运行中仅执行一次,且线程安全,所以也能够用它实现单例的线程安全
#import "Singleton.h"
static Singleton * singleton = nil;
@implementation Singleton
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [super allocWithZone:zone];
});
return singleton;
}
+ (instancetype) shareInstance {
return [self allocWithZone:nil];
}
- (id) copyWithZone:(NSZone *)zone;{
return self;
}