iOS中單例模式的兩種創建方法:GCD 和 @synchronize
1.GCD的方法
- 1.重寫allocWithZone:方法(注意不是重寫alloc方法,重寫了alloc 還是會執行allocWithZone:)
- 2.為需要創建単例的類創建一個獲取単例的類方法
- 3.最后不要忘記重寫copyWithZone:
- 4.<NSCopying> 沒必要寫,這邊只是為了快速敲出copyWithZone:方法
@interface JYPerson () //<NSCopying>
@end
@implementation JYPerson
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
+ (instancetype)sharedInstance
{
@synchronized(self) {
if (_instance == nil) {
_instance = [[self alloc] init];
}
}
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
2.GCD方法的宏實現
- 通過宏的方式省去一些不必要的代碼
- "" 是為了讓其預編譯指令了解是宏的內容
// .h文件
#define JYSingletonH + (instancetype)sharedInstance;
// .m文件
#define JYSingletonM \
\
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+ (instancetype)sharedInstance {\
\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
- (id)copyWithZone:(NSZone *)zone {\
\
return _instance;\
}
- 當然有些為了定義単例的名字可以將參數傳入
// .h文件
#define JYSingletonH(name) + (instancetype)shared##name;
// .m文件
#define JYSingletonM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
3.傳統寫法:
- 此方法需要注意的是線程安全(@synchronize)
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
@synchronized (self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
+ (instancetype)sharedInstance {
@synchronized (self) {
if (_instance == nil) {
_instance = [[self alloc] init];
}
}
return _instance;
}
- (id)copyWithZone:(NSZone *)zone {
return _instance;
}
4.注意事項:
- 重構GCD方法時采用的是宏方法,估計有人也會想到(多個類想獲得單例)能否通過
繼承
來實現呢? - 答:是不能的,現在可以嘗試一下我定義兩個萬能類 teacher, student 繼承于person,然后重寫単例方法。
NSLog(@"%@ %@", [JYStudent sharedInstance], [[JYStudent alloc] init]);
NSLog(@"%@ %@", [JYTeacher sharedInstance], [[JYTeacher alloc] init]);
- 打印了上述方法會發現全都是
JYStudent
類的對象,然后換個順序:會全都是JYTeacher
類的對象,以 "static id _instance; "為例static
聲明的是一個全局變量的指針,所以指向也是同一塊地址。所以會出現誰在前面都是誰的對象