ios什么是单例
时间:
欧东艳656由 分享
ios什么是单例
学习啦在线学习网 单例模式是ios里面经常使用的模式,例如
[UIApplicationsharedApplication] (获取当前应用程序对象)、[UIDevicecurrentDevice](获取当前设备对象);
学习啦在线学习网 单例模式的写法也很多。
第一种:
- static Singleton *singleton = nil;
- // 非线程安全,也是最简单的实现
- + (Singleton *)sharedInstance
- {
- if (!singleton) {
- // 这里调用alloc方法会进入下面的allocWithZone方法
- singleton = [[self alloc] init];
- }
- return singleton;
- }
- // 这里重写allocWithZone主要防止[[Singleton alloc] init]这种方式调用多次会返回多个对象
- + (id)allocWithZone:(NSZone *)zone
- {
- if (!singleton) {
- NSLog(@"进入allocWithZone方法了...");
- singleton = [super allocWithZone:zone];
- return singleton;
- }
- return nil;
- }
第二种:
- // 加入线程安全,防止多线程情况下创建多个实例
- + (Singleton *)sharedInstance
- {
- @synchronized(self)
- {
- if (!singleton) {
- // 这里调用alloc方法会进入下面的allocWithZone方法
- singleton = [[self alloc] init];
- }
- }
- return singleton;
- }
- // 这里重写allocWithZone主要防止[[Singleton alloc] init]这种方式调用多次会返回多个对象
- + (id)allocWithZone:(NSZone *)zone
- {
- // 加入线程安全,防止多个线程创建多个实例
- @synchronized(self)
- {
- if (!singleton) {
- NSLog(@"进入allocWithZone方法了...");
- singleton = [super allocWithZone:zone];
- return singleton;
- }
- }
- return nil;
- }
第三种:
- __strong static Singleton *singleton = nil;
- // 这里使用的是ARC下的单例模式
- + (Singleton *)sharedInstance
- {
- // dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的
- static dispatch_once_t pred = 0;
- dispatch_once(&pred, ^{
- singleton = [[super allocWithZone:NULL] init];
- });
- return singleton;
- }
- // 这里
- + (id)allocWithZone:(NSZone *)zone
- {
- /* 这段代码无法使用, 那么我们如何解决alloc方式呢?
- dispatch_once(&pred, ^{
- singleton = [super allocWithZone:zone];
- return singleton;
- });
- */
- return [self sharedInstance];
- }
- - (id)copyWithZone:(NSZone *)zone
- {
- return self;
- }