swift的單例設(shè)計模式
class SQLiteManager { // 設(shè)計單例對象
static let shareInstance : SQLiteManager = SQLiteManager()
}
OC的單例設(shè)計模式
// 一般使用為單例就足夠了,每次都用shareSingle創(chuàng)建對象//獲取單例
+ (instancetype)shareSingle {
static LLGSingleDayAndNight *single = nil;
// 添加同步鎖,一次只能一個線程訪問,如果有多個線程訪問,等待,一個訪問結(jié)束后下一個訪問
@synchronized (self) {
if (single == nil) {
single = [[LLGSingleDayAndNight alloc]init];
}
}
return single;
}
+(instancetype)sharedInstance{
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
新學(xué)習(xí)一種單例思想
靜態(tài)區(qū)存放單例對象.png