單例模式
這個(gè)單例模式的寫(xiě)法是參照MJ哥的寫(xiě)法。追求最精簡(jiǎn)。
1.只分配一塊內(nèi)存來(lái)創(chuàng)建對(duì)象。
2.提供一個(gè)類(lèi)方法,返回內(nèi)部唯一的一個(gè)對(duì)象。
3.最好保證init方法也是只初始化一次。
單例模式的目的
1.可以保證App在程序運(yùn)行中,一個(gè)類(lèi)只有唯一個(gè)實(shí)例,從而做到節(jié)約內(nèi)存。
2.在整個(gè)App程序中,這一份資源是共享的。
3.提供一個(gè)固定的實(shí)例創(chuàng)建方法。
單例模式在ARC\MRC環(huán)境下的寫(xiě)法是不相同的,所以需要兩份代碼。
在這里,我是看到MJ哥視頻后,發(fā)現(xiàn)單利模式結(jié)合宏,超級(jí)方便。
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
下面先來(lái)看下單利模式不結(jié)合宏的寫(xiě)法
/**
* 數(shù)據(jù)只初始化一次
*/
static id _instace;
//- (instancetype)init
//{
// static id object = nil;
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
//
// if (((object = [super init]) !=nil)) {
//
// //加載所需要的資源
//
// }
// });
// self = object;
// return self;
//}
- (instancetype)init
{
self = [super init];//如果單利是有繼承的話,則需要考慮init方法也是只需要加載一次。則用上面的方法。
if (self) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//加載所需要的音頻資源
});
}
return self;
}
/**
* 重寫(xiě)這個(gè)方法是為了控制內(nèi)存。只分配一份內(nèi)存。
*/
+(id)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}
/**
* 提供給外部的方法
*/
+(instancetype)sharedSingleTon{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc]init];
});
return _instace;
}
非ARC時(shí)候要手動(dòng)釋放內(nèi)存所以要寫(xiě)內(nèi)存管理方法。就是在ARC的代碼下添加多內(nèi)存管理方法。
/**
* 數(shù)據(jù)只初始化一次
*/
static id _instace;
//- (instancetype)init
//{
// static id object = nil;
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
//
// if (((object = [super init]) !=nil)) {
//
// //加載所需要的資源
//
// }
// });
// self = object;
// return self;
//}
- (instancetype)init
{
self = [super init];//如果單利是有繼承的話,則需要考慮init方法也是只需要加載一次。則用上面的方法。
if (self) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//加載所需要的音頻資源
});
}
return self;
}
/**
* 重寫(xiě)這個(gè)方法是為了控制內(nèi)存。只分配一份內(nèi)存。
*/
+(id)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}
/**
* 提供給外部的方法
*/
+(instancetype)sharedSingleTon{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc]init];
});
return _instace;
}
//實(shí)現(xiàn)內(nèi)存管理方法
- (id)retain{
return self;
}
- (NSUInteger)retainCount{
return 1;
}
- (oneway void)release{
}
- (id)autorelease{
return self;
}
下面是單利模式結(jié)合宏簡(jiǎn)直好用到爆炸,代碼精簡(jiǎn)
宏方法
// ## : 連接字符串和參數(shù)
#define Singleton_h(name) + (instancetype)shared##name;
#if __has_feature(objc_arc)// ARC時(shí)候調(diào)用的單例模式
#define Singleton_m(name) \
static id _instance; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}
#else// MRC時(shí)候調(diào)用的單例模式
#define Singleton_m(name) \
static id _instance; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)autorelease \
{ \
return _instance; \
} \
\
- (id)retain \
{ \
return _instance; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
\
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instance; \
}
#endif
單例.h文件
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface SingletonCode : NSObject
/**
* 這個(gè)就是宏創(chuàng)建的類(lèi)方法
*/
Singleton_h(TextSingleton);
@end
單例.m文件
#import "SingletonCode.h"
@implementation SingletonCode
- (instancetype)init
{
self = [super init];
if (self) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//加載所需要的資源文件
});
}
return self;
}
/**
* 這個(gè)就是宏實(shí)現(xiàn)的類(lèi)方法
*/
Singleton_m(TextSingleton);
@end