大家知道,单例模式是ios里面经常使用的模式,例如
[UIApplication sharedApplication] (获取当前应用程序对象)、 [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;
- static int count = 0;
- // 这里使用的是ARC下的单例模式
- + (Singleton *)sharedInstance
- {
- // dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的
- static dispatch_once_t pred = 0;
- dispatch_once(&pred, ^{
- singleton = [[self alloc] init];
- });
- return singleton;
- }
- // 这里
- + (id)allocWithZone:(NSZone *)zone
- {
- /* 这段代码无法使用, 那么我们如何解决alloc方式呢?
- dispatch_once(&pred, ^{
- singleton = [super allocWithZone:zone];
- return singleton;
- });
- */
- @synchronized(self)
- {
- // 我们暂时用计数器来解决这个问题(暂时没有想到更好的办法)
- if (!singleton && !count)
- {
- count++;
- singleton = [super allocWithZone:zone];
- return singleton;
- }
- }
- return nil;
- }
第三种是ARC下单例模式,也是比较方便的, 但是[[Singleton alloc] init];这种情况下就可以生成多个对象了,怎么办呢,我们暂时先使用计数的方式来防止多次创建实例,如果大家有更好的方法,可以留言给我。谢谢~