iOS开发之旅--深入讲解Singleton(单例设计模式)
2015-06-22 12:52
459 查看
singleton设计模式:
一、单例设计模式是什么?
简单讲,单例设计模式就是保证了某个类在程序中只有一个实例对象。
二、作用
1> 很直接的作用就是可以节省内存
2>可以将一些数据做到整个程序的全局共享。
3>一般工具类可以做成单例
三、实现
1)OC-MRC下:
2)OC-ARC下:
4 - (onewayvoid)release
5 - (instancetype)retain
6 - (instancetype)autorelease
7 - (NSUInteger)retainCount
3)Swift下:
仿写OC:
由以上看来,Swift占据优势。简便快捷高效。
在Object-C中,可以简单有效的创建单例对象。这需要下面的操作
一、单例设计模式是什么?
简单讲,单例设计模式就是保证了某个类在程序中只有一个实例对象。
二、作用
1> 很直接的作用就是可以节省内存
2>可以将一些数据做到整个程序的全局共享。
3>一般工具类可以做成单例
三、实现
1)OC-MRC下:
//将实力对象定义为静态id类型 static id instance; + (instancetype)sharedSingle { static dispatch_once_t once; dispatch_once(&once, ^{ instance = [[self alloc] init]; }); return instance; } //重写一下方法 + (instancetype)allocWithZone:(struct _NSZone*)zone { static dispatch_once_t once; dispatch_once(&once, ^{ instance = [super allocWithZone:zone]; }); return instance; } <pre name="code" class="objc">- (id)init { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super init]; }); + (id)copyWithZone:(struct _NSZone *)zone { return _instace; } + (id)mutableCopyWithZone:(struct _NSZone *)zone { return _instace; }- (instancetype)autorelease{ return instance;}- (instancetype)retain{ return instance;}- (NSUInteger)retainCount{ return 1;}
2)OC-ARC下:
//将实力对象定义为静态id类型 static id instance; + (instancetype)sharedSingle { static dispatch_once_t once; dispatch_once(&once, ^{ instance = [[self alloc] init]; }); return instance; } //重写一下方法 + (instancetype)allocWithZone:(struct _NSZone*)zone { static dispatch_once_t once; dispatch_once(&once, ^{ instance = [super allocWithZone:zone]; }); return instance; } - (id)init { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instace = [super init]; }); + (id)copyWithZone:(struct _NSZone *)zone { return _instace; } + (id)mutableCopyWithZone:(struct _NSZone *)zone { return _instace; }
arc模式下,不用手动管理内存,以下四个方法可以省略。
4 - (onewayvoid)release
5 - (instancetype)retain
6 - (instancetype)autorelease
7 - (NSUInteger)retainCount
3)Swift下:
仿写OC:
// 在 Swift 中不允许在函数中定义静态成员 static var instance: SingleTools? static var onceToken: dispatch_once_t = 0 // 1. 提供全局的访问点 class func sharedTools() -> SingleTools { dispatch_once(&onceToken) { () -> Void in instance = SoundTools() } return instance! }swift特有:
// 静态区的对象只能设置一次数值 // Swift 中的的单例写法和懒加载几乎一样 `static let` // 同样也是在第一次使用时,才会创建对象 static let sharedTools2: SingleTools = { print("创建对象") return SingleTools() }()
由以上看来,Swift占据优势。简便快捷高效。
在Object-C中,可以简单有效的创建单例对象。这需要下面的操作
// .h文件的实现 #define SingletonH(methodName) + (instancetype)shared##methodName; // .m文件的实现 #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宏定义单例方法之后,你什么都不用考虑了,在你的单例中,你只需要一句代码就能搞定。
single_h(@“你的单例类名称”)
相关文章推荐
- 使用Xcode 7 beta免费真机调试iOS应用程序
- iOS 数据持久化五-Core Data
- iOS 数据持久化二-对象归档
- iOS 数据持久化一-属性列表 Plist
- iOS 数据持久化三-NSUserDefaults
- iOS定位原理和使用建议
- 关于Core Location-ios定位
- CoreMotion框架-iOS设备的核心运动
- iOS开发系列--并行开发其实很容易
- IOS_核心动画 CoreAnimation
- iOS Collection 集合
- iOS 8 AutoLayOut入门
- IOS Socket编程
- [翻译] iOSSharedViewTransition
- ios开发-多线程GCD之详解
- ViewController 的 loadView, viewDidLoad, viewDidUnload 分别是在什么时候调用的?
- iOS-Block的简单使用
- iOS阶段学习第四天笔记(循环)
- iOS笔记052- Quartz2D-绘图
- iOS多线程的初步研究(八)-- dispatch队列