文藝求關注.png
啊喂,你遇到面試官要求你手寫單例模式了嗎张抄?那對于單例模式,你還記得多少洼怔?
-
<b>單例模式的作用</b>
- 可以保證在程序運行過程中署惯,一個類只有一個實例,而且該實力易于供外界訪問
- 方便控制實例個數(shù)镣隶,節(jié)約系統(tǒng)資源
-
<b>單例模式的使用場景</b>
- 在整個應用中极谊,共享一份資源(這份資源只需要創(chuàng)建初始化1次)
<b>ARC環(huán)境下的單例模式</b>
//.h文件
// 對外爆露類方法
// 原因:1.方便外部調(diào)用使用
// 2.為了表明身份
// 注意命名規(guī)范:shar + 類名 / default + 類名 / share / default
+ (instancetype) shareTools;
// .m文件
// 1.提供全局變量
static VtcTools *_instance;
// 2.從寫alloc方法 --> allocWithZone
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
- 第一種方法
// 加互斥鎖解決多線程訪問安全問題
@synchronized (self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone]; // 外界調(diào)用:VtcTools *t1 = [[VtcTools alloc] init];
}
}
- 第二種方法
// 一次性函數(shù),本身就是線程安全的
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone]; // 外界調(diào)用:VtcTools *t2 = [[VtcTools alloc] init];
});
return _instance;
}
// 3.提供類方法
+ (instancetype)shareTools {
return [[self alloc] init]; // 外界調(diào)用:VtcTools *t3 = [VtcTools shareTools];
}
// 4.嚴謹寫法(從寫以下兩種方法)
- (id)copyWithZone:(NSZone *)zone {
return _instance; // 外界調(diào)用方法:VtcTools *t4 = [t1 copy];
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _instance; // 外界調(diào)用方法:VtcTools *t5 = [t1 mutableCopy];
}
- <h5>MRC環(huán)境下的單例模式</h5>
// 修改環(huán)境 --> Build Settings (search) automatic A...R....C... NO
//.h文件
// 對外爆露類方法
// 原因:1.方便外部調(diào)用使用
// 2.為了表明身份
// 注意命名規(guī)范:shar + 類名 / default + 類名 / share / default
+ (instancetype) shareTools;
//.m文件
// 1.提供全局變量
static VtcTools *_instance;
// 2.從寫allocWithZone方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
// 第一種方法
// 加互斥鎖解決多線程訪問安全問題
@synchronized (self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone]; // 外界調(diào)用:VtcTools *t1 = [[VtcTools alloc] init];
}
}
// 第二種方法
// 本身就是線程安全的
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone]; // 外界調(diào)用:VtcTools *t2 = [[VtcTools alloc] init];
});
return _instance;
}
// 3.提供類方法
+ (instancetype)shareTools {
return [[self alloc] init]; // 外界調(diào)用:VtcTools *t3 = [VtcTools shareTools];
}
// 4.嚴謹寫法(從寫以下兩種方法)
- (id)copyWithZone:(NSZone *)zone {
return _instance; // 外界調(diào)用方法:VtcTools *t4 = [t1 copy];
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _instance; // 外界調(diào)用方法:VtcTools *t5 = [t1 mutableCopy];
}
#if __has_feature(objc_arc)
// 條件滿足ARC
#else
// MRC
- (oneway void)release {
}
- (instanceytpe)retain {
}
// 習慣性重寫
- (NSUInteger) retainCont {
return MAXFLOAT;
}
#endif
<b> 0财瘛轻猖!注意:在使用中,記得retain一次域那,就需要release一次~</b>
-<h5> A摺!注意:單例模式是不能使用繼承的</h5>
// 單例模式通用宏
//新建.h文件:Singleton
#define SingletonH(name) + (instancetype) share##name;
#if __has_feature(objc_arc)
// 條件滿足ARC
#define SingletonM(name) static VtcTools *_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 SingletonM(name) static VtcTools *_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 { \
} \
- (instanceytpe)retain { \
} \
- (NSUInteger) retainCont { \
return MAXFLOAT;\
}
#endif
// 外界調(diào)用:
// .h #import "Singleton.h"
// SingletonH (輸入name)
// .m SingletonM (輸入name)
關注一下又不會懷孕.png