單例模式
- 1.1 概念相關(guān)
(1)單例模式
在程序運行過程,一個類只有一個實例
(2)使用場合
在整個應(yīng)用程序中棚放,共享一份資源(這份資源只需要創(chuàng)建初始化1次)
- 1.2 ARC實現(xiàn)單例
(1)步驟
01 在類的內(nèi)部提供一個static修飾的全局變量
02 提供一個類方法藐石,方便外界訪問
03 重寫+allocWithZone方法违霞,保證永遠都只為單例對象分配一次內(nèi)存空間
04 嚴謹起見罐呼,重寫-copyWithZone方法和-MutableCopyWithZone方法
(2)相關(guān)代碼
//提供一個static修飾的全局變量壹店,強引用著已經(jīng)實例化的單例對象實例
static XMGTools *_instance;
//類方法诺舔,返回一個單例對象
+(instancetype)shareTools
{
//注意:這里建議使用self,而不是直接使用類名Tools(考慮繼承)
return [[self alloc]init];
}
//保證永遠只分配一次存儲空間
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
//使用GCD中的一次性代碼
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// _instance = [super allocWithZone:zone];
// });
//使用加鎖的方式奉狈,保證只分配一次存儲空間
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
/*
1. mutableCopy 創(chuàng)建一個新的可變對象卤唉,并初始化為原對象的值,新對象的引用計數(shù)為 1仁期;
2. copy 返回一個不可變對象桑驱。分兩種情況:(1)若原對象是不可變對象,那么返回原對象跛蛋,并將其引用計數(shù)加 1 熬的;(2)若原對象是可變對象,那么創(chuàng)建一個新的不可變對象赊级,并初始化為原對象的值押框,新對象的引用計數(shù)為 1。
*/
//讓代碼更加的嚴謹
-(nonnull id)copyWithZone:(nullable NSZone *)zone
{
// return [[self class] allocWithZone:zone];
return _instance;
}
-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone
{
return _instance;
}
- 1.3 MRC實現(xiàn)單例
(1)實現(xiàn)步驟
01 在類的內(nèi)部提供一個static修飾的全局變量
02 提供一個類方法理逊,方便外界訪問
03 重寫+allocWithZone方法橡伞,保證永遠都只為單例對象分配一次內(nèi)存空間
04 嚴謹起見,重寫-copyWithZone方法和-MutableCopyWithZone方法
05 重寫release方法
06 重寫retain方法
07 建議在retainCount方法中返回一個最大值
(2)配置MRC環(huán)境知識
01 注意ARC不是垃圾回收機制挡鞍,是編譯器特性
02 配置MRC環(huán)境:build setting ->搜索automatic ref->修改為NO
(3)相關(guān)代碼
//提供一個static修飾的全局變量骑歹,強引用著已經(jīng)實例化的單例對象實例
static XMGTools *_instance;
//類方法,返回一個單例對象
+(instancetype)shareTools
{
//注意:這里建議使用self,而不是直接使用類名Tools(考慮繼承)
return [[self alloc]init];
}
//保證永遠只分配一次存儲空間
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
//使用GCD中的一次性代碼
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// _instance = [super allocWithZone:zone];
// });
//使用加鎖的方式墨微,保證只分配一次存儲空間
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
return _instance;
}
//讓代碼更加的嚴謹
-(nonnull id)copyWithZone:(nullable NSZone *)zone
{
// return [[self class] allocWithZone:zone];
return _instance;
}
-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone
{
return _instance;
}
//在MRC環(huán)境下道媚,如果用戶retain了一次,那么直接返回instance變量翘县,不對引用計數(shù)器+1
//如果用戶release了一次最域,那么什么都不做,因為單例模式在整個程序運行過程中都擁有且只有一份锈麸,程序退出之后被釋放镀脂,所以不需要對引用計數(shù)器操作
-(oneway void)release
{
}
-(instancetype)retain
{
return _instance;
}
//慣用法,有經(jīng)驗的程序員通過打印retainCount這個值可以猜到這是一個單例
-(NSUInteger)retainCount
{
return MAXFLOAT;
}
- 1.4 通用版本
(1)有意思的對話
01 問:寫一份單例代碼在ARC和MRC環(huán)境下都適用忘伞?
答:可以使用條件編譯來判斷當(dāng)前項目環(huán)境是ARC還是MRC
02 問:條件編譯的代碼呢薄翅,么么噠沙兰?
//答:條件編譯
#if __has_feature(objc_arc)
//如果是ARC,那么就執(zhí)行這里的代碼1
#else
//如果不是ARC翘魄,那么就執(zhí)行代理的代碼2
#endif
03 問:在項目里面往往需要實現(xiàn)很多的單例鼎天,比如下載、網(wǎng)絡(luò)請求暑竟、音樂播放等等斋射,弱弱的問一句單例可以用繼承嗎?
答:單例是不可以用繼承的但荤,如果想一次寫就罗岖,四處使用,那么推薦親使用帶參數(shù)的宏定義啦腹躁!
04 問:宏定義怎么弄桑包?
答:這個嘛~~回頭看一眼我的代碼咯,親潜慎。
(2)使用帶參數(shù)的宏完成通用版單例模式代碼
01 注意條件編譯的代碼不能包含在宏定義里面
02 宏定義的代碼只需要寫一次就好捡多,之后直接拖到項目中用就OK
一些單例:
1、UIApplication(應(yīng)用程序?qū)嵗?
獲取方式:[UIApplication sharedApplication]
詳細:http://www.cnblogs.com/hissia/p/5678518.html
2铐炫、NSNotificationCenter(消息中心)
獲取方式:[NSNotificationCenter defaultCenter]
常用的通知模式
3垒手、NSFileManager(文件管理)
獲取方式:[NSFileManager defaultManager]
4、NSUserDefaults(偏好設(shè)置)
獲取方式:[NSUserDefaults standardUserDefaults]
詳細:http://www.cnblogs.com/hissia/p/5642405.html
5倒信、NSURLCache(請求緩存)
獲取方式:[NSURLCache sharedURLCache]
6科贬、NSHTTPCookieStorage(應(yīng)用程序cookies池)
獲取方式:[NSHTTPCookieStorage sharedHTTPCookieStorage]
7、NSURLSession(發(fā)送請求時候用的)
獲取方式:[NSURLSession sharedSession]
8鳖悠、UIMenuController(彈出的菜單可以選擇榜掌,復(fù)制,剪切乘综,粘貼的功能)
獲取方式:[UIMenuController sharedMenuController]