博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C(iOS)严格单例模式正确实现
阅读量:4657 次
发布时间:2019-06-09

本文共 3101 字,大约阅读时间需要 10 分钟。

注:本文所有权归作者所有,转载请注明出处  

  当希望在一个应用程序中某个类的对象只能存在一个的时候就可以考虑用单例模式来实现,单例模式在C++中比较容易实现(只需把构造函数声明为private),而在Objective-C中对象可以通过NSObject的alloc来产生,所以需要编写一些额外的代码来确保对象的唯一性,考虑到现在编写iOS APP代码几乎都是ARC方式,且GCD也已经被用烂了,故本文给出一种利用GCD技术来实现严格单例模式的ARC版本,具体代码如下所示,所有的注意点都写在了注释里面:

1 Singleton.h 2 @interface Singleton : NSObject 3 @property(nonatomic,strong) NSString *name; 4 +(Singleton*)defaultManager; 5 @end 6  7  8 Singleton.m 9 @implementation Singleton10 //单例类的静态实例对象,因对象需要唯一性,故只能是static类型11 static Singleton *defaultManager = nil;12 13 /**单例模式对外的唯一接口,用到的dispatch_once函数在一个应用程序内只会执行一次,且dispatch_once能确保线程安全14 */15 +(Singleton*)defaultManager16 {17     static dispatch_once_t token;18     dispatch_once(&token, ^{19         if(defaultManager == nil)20         {21             defaultManager = [[self alloc] init];22         }23     });24     return defaultManager;25 }26 27 /**覆盖该方法主要确保当用户通过[[Singleton alloc] init]创建对象时对象的唯一性,alloc方法会调用该方法,只不过zone参数默认为nil,因该类覆盖了allocWithZone方法,所以只能通过其父类分配内存,即[super allocWithZone:zone]28  */29 +(id)allocWithZone:(struct _NSZone *)zone30 {31    static dispatch_once_t token;32     dispatch_once(&token, ^{33         if(defaultManager == nil)34         {35             defaultManager = [super allocWithZone:zone];36         }37     });38     return defaultManager;39 }40 //自定义初始化方法,本例中只有name这一属性41 - (instancetype)init42 {43     self = [super init];44     if(self)45     {46         self.name = @"Singleton";47     }48     return self;49 }50 51 //覆盖该方法主要确保当用户通过copy方法产生对象时对象的唯一性52 - (id)copy53 {54     return self;55 }56 57 //覆盖该方法主要确保当用户通过mutableCopy方法产生对象时对象的唯一性58 - (id)mutableCopy59 {60     return self;61 }62 //自定义描述信息,用于log详细打印63 - (NSString *)description64 {65     return [NSString stringWithFormat:@"memeory address:%p,property name:%@",self,self.name];66 }

测试代码如下:

1 Singleton *defaultManagerSingleton =[Singleton defaultManager]; 2 NSLog(@"defaultManagerSingleton:\n%@",defaultManagerSingleton); 3 Singleton *allocSingleton = [[Singleton alloc] init]; 4 NSLog(@"allocSingleton:\n%@",allocSingleton); 5 Singleton *copySingleton = [allocSingleton copy]; 6 NSLog(@"copySingleton:\n%@",copySingleton); 7 Singleton *mutebleCopySingleton = [allocSingleton mutableCopy]; 8 NSLog(@"mutebleCopySingleton:\n%@",mutebleCopySingleton); 9 10 //打印结果11 2015-10-11 21:48:34.722 Singleton[1941:214584] defaultManagerSingleton:12 memeory address:0x7fa6d1591530,property name:Singleton13 2015-10-11 21:48:34.727 Singleton[1941:214584] allocSingleton:14 memeory address:0x7fa6d1591530,property name:Singleton15 2015-10-11 21:48:34.727 Singleton[1941:214584] copySingleton:16 memeory address:0x7fa6d1591530,property name:Singleton17 2015-10-11 21:48:34.727 Singleton[1941:214584] mutebleCopySingleton:18 memeory address:0x7fa6d1591530,property name:Singleton

从打印结果来看通过 [Singleton defaultManager]、[[Singleton alloc] init]、[allocSingleton copy]、[allocSingleton mutableCopy]这四种方法生成的对象地址都是0x7fa6d1591530,即表明对象是同一个,也就实现了严格单例模式,加上GCD是线程安全的所以在多线程中也能保证对象的唯一性。

另:在学习Objective-C编写单例模式时看到网上好多人都借用苹果官方的实现方式,但我自己始终没搜到官方的实现Sample代码,如果你知道麻烦把网址给我发下,谢谢哈~

转载于:https://www.cnblogs.com/NerdFooProgrammer/p/4870260.html

你可能感兴趣的文章
Spring Boot入门
查看>>
【OpenCV学习】椭圆拟合
查看>>
探索C#之6.0语法糖剖析
查看>>
练习2.1
查看>>
通过Word发布博客园文章
查看>>
[转]关于语音的一些基本概念
查看>>
HTML杂项和HTML废弃标签
查看>>
21-高级属性之内建方法(2)
查看>>
51-os库函数之应用(1)
查看>>
android应用安全——代码安全(android代码混淆)
查看>>
web页面增、删、改
查看>>
第十四周学习进度表
查看>>
web网站性能优化整理
查看>>
年度最佳电视剧
查看>>
C#实现调用默认浏览器打开多个网页
查看>>
qt的登录设置(转)
查看>>
iOS ADBannerView的简单使用
查看>>
有关std::map和std::vector的使用
查看>>
差点要重新装机,驱动问题解决一例
查看>>
ThinkPHP3.1 常量参考
查看>>