iOS中的单例模式
来源:互联网 发布:听音识曲哪个软件更好 编辑:程序博客网 时间:2024/06/05 20:27
- 技巧: 使用宏定义快速创建单例
- #if __has_feature(objc_arc) 判断当前项目的环境是否是ARC
- 注意: 单例不可以被继承
单例模式
在程序运行过程,一个类只有一个实例,无论如何程序运行过程中只有一份实例
使用场合
在整个应用程序中,共享一份资源(这份资源只需要创建初始化1次)
使用技巧
在项目里面往往需要实现很多的单例,比如下载、网络请求、音乐播放等等,单例可以用继承吗?
单例是不可以用继承的,如果想一次写就,四处使用,那么推荐使用带参数的宏定义啦!
创建步骤(ARC)
01 在类的内部提供一个static修饰的全局变量
02 提供一个类方法,方便外界访问
03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法
//提供一个static修饰的全局变量,强引用着已经实例化的单例对象实例static Tools *_instance;//类方法,返回一个单例对象+(instancetype)shareTools{ //注意:这里建议使用self,而不是直接使用类名Tools(考虑继承) return [[self alloc]init];}//保证永远只分配一次存储空间+(instancetype)allocWithZone:(struct _NSZone *)zone{ //使用GCD中的一次性代码// static dispatch_once_t onceToken;// dispatch_once(&onceToken, ^{// _instance = [super allocWithZone:zone];// }); //使用加锁的方式,保证只分配一次存储空间 @synchronized(self) { if (_instance == nil) { _instance = [super allocWithZone:zone]; } } return _instance;}/*1. mutableCopy 创建一个新的可变对象,并初始化为原对象的值,新对象的引用计数为 1;2. copy 返回一个不可变对象。分两种情况:(1)若原对象是不可变对象,那么返回原对象,并将其引用计数加 1 ;(2)若原对象是可变对象,那么创建一个新的不可变对象,并初始化为原对象的值,新对象的引用计数为 1。*///让代码更加的严谨-(nonnull id)copyWithZone:(nullable NSZone *)zone{// return [[self class] allocWithZone:zone]; return _instance;}-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone{ return _instance;}
创建步骤(MRC)
01 在类的内部提供一个static修饰的全局变量
02 提供一个类方法,方便外界访问
03 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
04 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法
05 重写release方法
06 重写retain方法
07 建议在retainCount方法中返回一个最大值
//提供一个static修饰的全局变量,强引用着已经实例化的单例对象实例static Tools *_instance;//类方法,返回一个单例对象+(instancetype)shareTools{ //注意:这里建议使用self,而不是直接使用类名Tools(考虑继承) return [[self alloc]init];}//保证永远只分配一次存储空间+(instancetype)allocWithZone:(struct _NSZone *)zone{ //使用GCD中的一次性代码// static dispatch_once_t onceToken;// dispatch_once(&onceToken, ^{// _instance = [super allocWithZone:zone];// }); //使用加锁的方式,保证只分配一次存储空间 @synchronized(self) { if (_instance == nil) { _instance = [super allocWithZone:zone]; } } return _instance;}//让代码更加的严谨-(nonnull id)copyWithZone:(nullable NSZone *)zone{// return [[self class] allocWithZone:zone]; return _instance;}-(nonnull id)mutableCopyWithZone:(nullable NSZone *)zone{ return _instance;}//在MRC环境下,如果用户retain了一次,那么直接返回instance变量,不对引用计数器+1//如果用户release了一次,那么什么都不做,因为单例模式在整个程序运行过程中都拥有且只有一份,程序退出之后被释放,所以不需要对引用计数器操作-(oneway void)release{}-(instancetype)retain{ return _instance;}//惯用法,有经验的程序员通过打印retainCount这个值可以猜到这是一个单例-(NSUInteger)retainCount{ return MAXFLOAT;}
通过宏定义创建单例
#define interfaceSingle(name) + (instancetype)share##name#if __has_feature(objc_arc)// 如果是ARC#define implementationSingle(name) + (instancetype)share##name \{ \ return [[self alloc] init]; \} \static id _instance; \+ (instancetype)allocWithZone:(struct _NSZone *)zone \{ \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ return _instance; \} \- (id)copyWithZone:(NSZone *)zone \{ \ return _instance; \} \- (id)mutableCopyWithZone:(NSZone *)zone \{ \ return _instance; \}#else// 如果不是ARC#define implementationSingle(name) + (instancetype)share##name \{ \return [[self alloc] init]; \} \static id _instance; \+ (instancetype)allocWithZone:(struct _NSZone *)zone \{ \static dispatch_once_t onceToken; \dispatch_once(&onceToken, ^{ \_instance = [super allocWithZone:zone]; \}); \return _instance; \} \- (id)copyWithZone:(NSZone *)zone \{ \return _instance; \} \- (id)mutableCopyWithZone:(NSZone *)zone \{ \return _instance; \}\- (oneway void)release \{} \- (instancetype)retain \{ \ return _instance; \} \- (NSUInteger)retainCount \{ \ return MAXFLOAT; \}#endif
0 0
- iOS中的单例模式
- IOS中的单例模式
- IOS 中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- IOS中的单例模式
- IOS中的单例模式
- iOS中的单例模式
- iOS 中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- ios中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- iOS中的单例模式
- 判断是否是平衡二叉树
- 51nod1083 矩阵取数问题
- 【poj2152】Fire 树形DP
- 经典题:不断求第k大数(巧用优先队列)(网赛)(4006)
- HDU2563——统计问题
- iOS中的单例模式
- linux c 实现线程池
- Markdown的最基本最常用的语法
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- HDOJ 2186-悼念512汶川大地震遇难同胞——一定要记住我爱你
- linux 安装 nginx
- utilities(C++)——单例(Singleton)
- 文档对象模型DOM
- XML与DTD