// 1.在该类中定义一个静态的全局变量,防止被外部用extren访问
static id _instance;
/**设计模式
static : 修饰变量缓存
1> 修饰全局变量安全
* 全局变量的做用域仅限于当前文件内部,其余文件不能用extren关键字访问cookie
2> 修饰局部变量 : 多线程
* 局部变量的生命周期 跟 全局变量 相似spa
* 可是不能改变做用域线程
* 能保证局部变量永远只初始化1次,在程序运行过程当中,永远只有1份内存设计
*/code
/** * 2.重写它这个类的llocWithZone:方法,确保只为你这个类分配一次内存地址 * alloc方法内部会调用这个方法 */ + (id)allocWithZone:(struct _NSZone *)zone { if (_instance == nil) { // 防止频繁加锁 @synchronized(self) { // 加锁,避免在多线程中出错而建立多个对象 if (_instance == nil) { // 防止建立屡次 _instance = [super allocWithZone:zone]; } } } return _instance; } // 3.提供一个shared方法让外界调用这个单例(通常单例都会提供这个方法),确保只init一次 + (instancetype)sharedMusicTool { if (_instance == nil) { // 防止频繁加锁 @synchronized(self) { if (_instance == nil) { // 防止建立屡次 _instance = [[self alloc] init]; } } } return _instance; } // 4.重写copyWithZone:方法,避免使用copy时建立多个对象 - (id)copyWithZone:(NSZone *)zone { return _instance; }
2.饿汉模式(当类加载到OC运行环境中(内存)时,就会调用一次生成这个单例(一个类只会加载一次))对象
// 1.在该类中定义一个静态的全局变量,防止被外部用extren访问 static id _instance; /** * 2.重写它这个类的llocWithZone:方法,这里不用加锁,由于程序刚启动,线程还没加载,不会出现线程不安全的问题 */ + (id)allocWithZone:(struct _NSZone *)zone { if (_instance == nil) { // 防止建立屡次 _instance = [super allocWithZone:zone]; } return _instance; } // 3.提供一个shared方法让外界调用这个单例(通常单例都会提供这个方法) + (instancetype)sharedMusicTool { return _instance; } // 4.重写copyWithZone:方法,避免使用copy时建立多个对象 - (id)copyWithZone:(NSZone *)zone { return _instance; } // 5.重写load这个类方法,在里面alloc它 // 这个方法在程序启动,加载类的时候会调用一次 + (void)load { _instance = [[self alloc] init]; }
// 1. static id _instance; /** * 2.用GCD的dispatch_once方法重写 */ + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } // 3. + (instancetype)sharedMusicTool { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } // 4. - (id)copyWithZone:(NSZone *)zone { return _instance; }
// 前面与ACR同样,接着重写如下方法
// 不作release
- (oneway void)release { } // retain以后仍是本身一份 - (id)retain { return self; } // 计数器永远为1 - (NSUInteger)retainCount { return 1; } // 防止被放进自动计数池释放 - (id)autorelease { return self; }
// 新建一个.h文件或pch文件,把一下代码copy进去。在须要实现单例的类中import进去并使用宏(括号里面传相应的类名)便可
// 在.h文件使用的宏定义 #define HJSingletonH(name) + (instancetype)shared##name; #if __has_feature(objc_arc) // 当前的编译器环境是ARC // 在.m文件使用的宏定义 #define HJSingletonM(name) \ static id _instace; \ \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super allocWithZone:zone]; \ }); \ return _instace; \ } \ \ + (instancetype)shared##name \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [[self alloc] init]; \ }); \ return _instace; \ } \ \ - (id)copyWithZone:(NSZone *)zone \ { \ return _instace; \ } #else // 当前的编译器环境是MRC // 在.m文件使用的宏定义 #define HJSingletonM(name) \ static id _instace; \ \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super allocWithZone:zone]; \ }); \ return _instace; \ } \ \ + (instancetype)shared##name \ { \ 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;} #endif