MRC環(huán)境-單例實(shí)現(xiàn)
.h文件實(shí)現(xiàn)
@interface DemoSoundTool : NSObject
+ (instancetype)sharedSoundTool;
.m文件實(shí)現(xiàn)
實(shí)現(xiàn)一個(gè)類方法
重寫五個(gè)系統(tǒng)方法
#import "DemoSoundTool.h"
@implementation DemoSoundTool
static DemoSoundTool *_soundTool = nil;
/**
* alloc方法內(nèi)部會(huì)調(diào)用allocWithZone:
* zone : 系統(tǒng)分配給app的內(nèi)存
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_soundTool == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ // 安全(這個(gè)代碼只會(huì)被調(diào)用一次)
_soundTool = [super allocWithZone:zone];
});
}
return _soundTool;
}
- (id)init
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_soundTool = [super init];
});
return _soundTool;
}
+ (instancetype)sharedSoundTool
{
return [[self alloc] init];
}
- (oneway void)release
{
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return 1;
}
ARC環(huán)境下- 單例實(shí)現(xiàn)
不重寫 release, retain, retainCount這三個(gè)函數(shù)即可, 其他 和 MRC一樣
當(dāng)需要定義多個(gè) 單例的時(shí)候, 每個(gè)單例內(nèi)部實(shí)現(xiàn)基本一樣, 這時(shí)可以[ 抽取一個(gè)宏 ] 實(shí)現(xiàn)單例, 減少代碼量開支
定義一個(gè)頭文件singleton.h
// 幫助實(shí)現(xiàn)單例設(shè)計(jì)模式
// .h文件的實(shí)現(xiàn)
#define SingletonH(methodName) + (instancetype)shared##methodName;
// .m文件的實(shí)現(xiàn)
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#else // 不是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#endif
宏定義之后,就可以一句話搞定 單例
例如在MRC環(huán)境下 , 單例模式可以這樣實(shí)現(xiàn)
.h文件實(shí)現(xiàn)
#import "Singleton.h"
@interface DemoSoundTool : NSObject
SingletonH(SoundTool)
.m文件實(shí)現(xiàn)
#import "Singleton.h"
#import "DemoSoundTool.h"
@implementation DemoSoundTool
//這樣一句話就搞定了
SingletonM(SoundTool)
@end