這篇文章的主要內(nèi)容包括:
1、在ARC中完成一個(gè)單例模式的三步
2、在MRC中完成一個(gè)單例模式的三步
3、單例模式通用宏(最重要)
單例模式是一種常用的軟件設(shè)計(jì)模式。
在它的核心結(jié)構(gòu)中只包含一個(gè)被稱(chēng)為單例的特殊類(lèi)。通過(guò)單例模式可以保證系統(tǒng)中一個(gè)類(lèi)只有一個(gè)實(shí)例,節(jié)約系統(tǒng)的資源。
-
單例模式的應(yīng)用場(chǎng)景
在整個(gè)程序中,共享一份資源,這個(gè)資源只被初始化一次。
-
在ARC中完成一個(gè)單例模式一共有三步:
0.提供靜態(tài)全局變量
1.重寫(xiě)allocWithZone
2.提供一個(gè)類(lèi)方法
3.重寫(xiě)copyWithZone和MutableCopyWithZone
#import "XMGTool.h"
@implementation XMGTool
//0.提供全局變量
static XMGTool *_instance;
//1.重寫(xiě)alloc方法
+(instancetype)allocWithZone:(struct _NSZone *)zone{
// 懶加載 永遠(yuǎn)只分配一次存儲(chǔ)空間
// 如果多個(gè)線(xiàn)程同時(shí)alloc,訪(fǎng)問(wèn)同一塊資源,可能涉及線(xiàn)程安全的問(wèn)題,所以我們可以加上一把互斥鎖
// @synchronized (self) {
//
// }if(_instance==nil){
// _instance=[super allocWithZone:zone];
// }
// 方法二:GCD一次性代碼
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance=[super allocWithZone:zone];
});
return _instance;
}
//2.提供一個(gè)類(lèi)方法
//類(lèi)方法的好處在于:外界方便訪(fǎng)問(wèn);表明身份
//注意命名規(guī)范share+類(lèi)名/default+類(lèi)名
+(instancetype)shareTool{
return [[self alloc]init];
//這里alloc就調(diào)用了上面重寫(xiě)的alloc方法 保證只創(chuàng)建一個(gè)對(duì)象
}
//3.重寫(xiě)copy和mutable方法
-(id)copyWithZone:(NSZone *)zone{
//對(duì)象方法,運(yùn)行之前已經(jīng)有對(duì)象存在了,所以直接返回就可以
return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return _instance;
}
@end
這樣,在控制器中不論用以下任何一種方式創(chuàng)建對(duì)象,地址都是相同的,因?yàn)橹粍?chuàng)建一次:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.重寫(xiě)allocWithZone
//單例模式就是不管過(guò)程中alloc init多少次 new多少次 都始終是那一個(gè)對(duì)象
//單例模式的實(shí)現(xiàn)方法:重寫(xiě)alloc方法
XMGTool *t1=[[XMGTool alloc]init];
XMGTool *t2=[[XMGTool alloc]init];
XMGTool *t3=[XMGTool new];
//2.提供一個(gè)類(lèi)方法
XMGTool *t4=[XMGTool shareTool];
//3.重寫(xiě)copyWithZone和MutableCopyWithZone
//注意:這兩個(gè)方法都是對(duì)象方法
XMGTool *t5=[t1 copy];
XMGTool *t6=[t1 mutableCopy];
NSLog(@"t1:%p----t2:%p----t3:%p----t4:%p----t5:%p----t6:%p",t1,t2,t3,t4,t5,t6);
}
-
在MRC中實(shí)現(xiàn)單例模式也一共三步
非ARC模式事實(shí)上離我們已經(jīng)很遙遠(yuǎn)了
事實(shí)上,就是重寫(xiě)release和retain以及retainCount方法
這里可以使用一個(gè)小技巧 ——條件編譯(注意,條件編譯不可以用在宏中)
進(jìn)行判斷 如果不滿(mǎn)足ARC就執(zhí)行else和endif之間的
//上接ARC的三個(gè)方法
#if __has_feature(objc_arc)
#else
//MRC
-(oneway void)release{
}
-(instancetype)retain{
return _instance;
}
//一個(gè)約定俗成的習(xí)慣
-(NSUInteger)retainCount{
return MAXFLOAT;
}
#endif
-
單例模式通用宏
其實(shí)以上兩個(gè)單例模式都不好
下面介紹在 ARC、MRC都可以實(shí)現(xiàn)單例模式的通用宏
用它實(shí)現(xiàn)代碼的復(fù)用,也就是說(shuō)把單例模式抽取出來(lái),以后用到的時(shí)候,在類(lèi)的h和m文件中包含這個(gè)宏就可以了
以下是宏中的代碼(條件判斷編譯不可以放在宏中):
#define SingleH(name) +(instancetype)share##name;
#if __has_feature(objc_arc)
//ARC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance=[super allocWithZone:zone];\
});\
return _instance;\
}\
+(instancetype)share##name{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}
#else
//MRC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance=[super allocWithZone:zone];\
});\
return _instance;\
}\
+(instancetype)share##name{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(oneway void)release{\
}\
-(instancetype)retain{\
return _instance;\
}\
-(NSUInteger)retainCount{\
return MAXFLOAT;\
}
#endif
引用示例: