1.单例模式的作用
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
第一种:懒汉式生成单例
/**
static : 修饰变量
1> 修饰全局变量
* 全局变量的作用域仅限于当前文件内部
2> 修饰局部变量 :
* 局部变量的生命周期 跟 全局变量 类似
* 但是不能改变作用域
* 能保证局部变量永远只初始化1次,在程序运行过程中,永远只有1分内存
*/
static id _instance;
只允许内部访问。防止外部文件恶意篡改我的数据
引用某个全局变量(并非定义)
extern id _instance;
_instance = nil;
extern是在整个项目中查找这个变量
+ (id)allocWithZone:(struct _NSZone *)zone,所以在创建对象的时候,我们要拦截创建信息
/**
* alloc方法内部会调用这个方法
*/
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
+ (instancetype)sharedMusicTool
{
if (_instance == nil) { // 防止频繁加锁
@synchronized(self) {
if (_instance == nil) { // 防止创建多次
_instance = [[self alloc] init];
}
}
}
return _instance;
}
// copy 有可能会产生新的对象
// copy方法内部会调用- copyWithZone:
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
方式二:
// 用来保存唯一的单例对象
static id _instace;
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}
+ (instancetype)sharedDataTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc] init];
});
return _instace;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instace;
}
**
* 当类加载到OC运行时环境中(内存),就会调用一次(一个类只会加载1次)
*/
+ (void)load
{
_instance = [[self alloc] init];
}
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (_instance == nil) { // 防止创建多次
_instance = [super allocWithZone:zone];
}
return _instance;
}
+ (instancetype)sharedSoundTool
{
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
///**
// * 当第一次使用这个类的时候才会调用
// */
//+ (void)initialize
//{
// NSLog(@"HMSoundTool---initialize");
//}
- (id)retain { return self; }
- (NSUInteger)retainCount { return 1; }
- (oneway void)release{}
- (id)autorelease { return self; }
@implementation HMDataTool
// 用来保存唯一的单例对象
static id _instace;
+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}
+ (instancetype)sharedDataTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc] init];
});
return _instace;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instace;
}
- (oneway void)release { }
- (id)retain { return self; }
- (NSUInteger)retainCount { return 1;}
- (id)autorelease { return self;}
// .h文件
#define HMSingletonH + (instancetype)sharedInstance;
// .m文件
#define HMSingletonM \
static id _instance; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)sharedInstance \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
//ARC与非ARC混编
//#if __has_feture(objc_arc)
//ARC编译
//#else
//非ARC编译
//#endif
第二种表现形式:
// .h文件
#define HMSingletonH(name) +(instancetype)shared##name;
// .m文件
#define HMSingletonM \
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; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
//ARC与非ARC混编
//#if __has_feture(objc_arc)
//ARC编译
//#else
//非ARC编译
//#endif
.h文件中
#import <Foundation/Foundation.h>
@interface HMMovieTool : NSObject
HMSingletonH(MovieTool)
@end
#import "HMMovieTool.h"
@implementation HMMovieTool
HMSingletonM(MovieTool)
@end