- 單例的作用 :
個(gè)人的理解就是內(nèi)存地址只分配一次,不管你是在主線程調(diào)用還是在子線程調(diào)用择克,方便我們使用的某個(gè)對(duì)象 恬总。比如 登陸的用戶模型 你可能用戶名 昵稱等等的隨時(shí)都在用 但是呢又沒(méi)有必要持久化存儲(chǔ)的這個(gè)時(shí)候可能用到單例。 - 單例的模式 :懶漢是單例
解釋:顧名思義 就是我們用到的時(shí)候才會(huì)加載肚邢,其實(shí)我們平時(shí)也很多時(shí)候用到壹堰。
代碼:
// 1.在該類中定義一個(gè)靜態(tài)的全局變量,防止被外部用extren訪問(wèn)
static id _instance;
/**
static : 修飾變量
1> 修飾全局變量
* 全局變量的作用域僅限于當(dāng)前文件內(nèi)部拭卿,其他文件不能用extren關(guān)鍵字訪問(wèn)
2> 修飾局部變量 :
* 局部變量的生命周期 跟 全局變量 類似
* 但是不能改變作用域
* 能保證局部變量永遠(yuǎn)只初始化1次,在程序運(yùn)行過(guò)程中贱纠,永遠(yuǎn)只有1分內(nèi)存
*/
/**
* 2.重寫它這個(gè)類的llocWithZone:方法峻厚,確保只為你這個(gè)類分配一次內(nèi)存地址
* alloc方法內(nèi)部會(huì)調(diào)用這個(gè)方法
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil) { // 防止頻繁加鎖
@synchronized(self) { // 加鎖,避免在多線程中出錯(cuò)而創(chuàng)建多個(gè)對(duì)象
if (_instance == nil) { // 防止創(chuàng)建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
// 3.提供一個(gè)shared方法讓外界調(diào)用這個(gè)單例(一般單例都會(huì)提供這個(gè)方法)谆焊,確保只init一次
+ (instancetype)sharedMusicTool
{
if (_instance == nil) { // 防止頻繁加鎖
@synchronized(self) {
if (_instance == nil) { // 防止創(chuàng)建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
// 4.重寫copyWithZone:方法惠桃,避免使用copy時(shí)創(chuàng)建多個(gè)對(duì)象
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
解釋:其實(shí)要將寫好點(diǎn) 我們還需要實(shí)現(xiàn)他的mutablecopy 方法,為什么枷鎖 就是防止多線程出錯(cuò)從而不是創(chuàng)建不是一次
- 單例之餓漢式加載
自己的理解:就是當(dāng)加載的oc運(yùn)行環(huán)境時(shí)加載一次懊渡,每個(gè)類只是加載一次刽射,這個(gè)我們平時(shí)不常用,但是作為一個(gè)程序員 我們有必要深究并且知道為什么剃执。
代碼:
// 1.在該類中定義一個(gè)靜態(tài)的全局變量,防止被外部用extren訪問(wèn)
static id _instance;
/**
* 2.重寫它這個(gè)類的llocWithZone:方法誓禁,這里不用加鎖,因?yàn)槌绦騽倖?dòng)肾档,線程還沒(méi)加載摹恰,不會(huì)出現(xiàn)線程不安全的問(wèn)題
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil) { // 防止創(chuàng)建多次
_instance = [super allocWithZone:zone];
}
return _instance;
}
// 3.提供一個(gè)shared方法讓外界調(diào)用這個(gè)單例(一般單例都會(huì)提供這個(gè)方法)
+ (instancetype)sharedMusicTool
{
return _instance;
}
// 4.重寫copyWithZone:方法,避免使用copy時(shí)創(chuàng)建多個(gè)對(duì)象
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
// 5.重寫load這個(gè)類方法怒见,在里面alloc它
// 這個(gè)方法在程序啟動(dòng)俗慈,加載類的時(shí)候會(huì)調(diào)用一次
+ (void)load
{
_instance = [[self alloc] init];
}
解釋:這里不用枷鎖 沒(méi)事的 因?yàn)槌绦騽倖?dòng)線程還沒(méi)有加載 所用不用枷鎖 為了更加安全 枷鎖更好了。
- 單例模式- 在MRC情況下:
說(shuō)明:我剛開(kāi)始學(xué)習(xí)ios的時(shí)候 那時(shí)候xcode還是5 現(xiàn)在都xocde9了 幾乎所有的公司都是ARC了遣耍,所以這個(gè)我們可以不用多管 但是必須的得知道闺阱,下面我會(huì)在下面用一個(gè)宏說(shuō)明 - 單例模式-gcd的方式
解釋:我們主要用到的是gcd的方式 不再用枷鎖的方式 因?yàn)槲覀€(gè)人覺(jué)得系統(tǒng)在內(nèi)部已經(jīng)給我們實(shí)現(xiàn)好了 肯定考慮到線程的問(wèn)題 以及其他的問(wèn)題 我們沒(méi)有想到的系統(tǒng)也給我們處理了。
代碼實(shí)現(xiàn):
// 1.
static id _instance;
/**
* 2.用GCD的dispatch_once方法重寫
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
// 3.
+ (instancetype)sharedMusicTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
// 4.
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
- 最后我們用一個(gè)宏的點(diǎn)h文件把所有的都包括了舵变,以后我們只需要把這個(gè)宏都帶進(jìn)去就可以了
// 在.h文件使用的宏定義
#define HJSingletonH(name) + (instancetype)shared##name;
#if __has_feature(objc_arc)
// 當(dāng)前的編譯器環(huán)境是ARC
// 在.m文件使用的宏定義
#define HJSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
}
#else
// 當(dāng)前的編譯器環(huán)境是MRC
// 在.m文件使用的宏定義
#define HJSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} \
- (oneway void)release {} \
- (id)retain {return self;} \
- (NSUInteger)retainCount {return 1;} \
- (id)autorelease {return self;}
#endif
解釋:這個(gè)帶ARC和MRC的