标签:Singleton 22 iOS three shareSingleton two 单例 2015 写法
原文链接:https://my.oschina.net/zyboy/blog/617435单例模式在iOS开发中是最为常用的模式之一,在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。一般情况下,许多人都是按下面的方式写单例模式:
#import "Singleton.h"
@implementation Singleton
static Singleton *shareSingleton = nil;
+ (instancetype)shareSingleton {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareSingleton = [[self alloc] init];
});
return shareSingleton;
}
@end
测试一下这种方法可行不:
Singleton *one = [Singleton shareSingleton];
NSLog(@"one = %@", one);
Singleton *two = [Singleton shareSingleton];
NSLog(@"two = %@", two);
Singleton *three = [[Singleton alloc] init];
NSLog(@"three = %@", three);
控制台输出内容:
2015-12-22 15:31:38.230 Singleton[2264:1996979] one = <Singleton: 0x7fe598f4d3c0>
2015-12-22 15:31:38.230 Singleton[2264:1996979] two = <Singleton: 0x7fe598f4d3c0>
2015-12-22 15:31:38.231 Singleton[2264:1996979] three = <Singleton: 0x7fe598edc570>
由此可见,只有调用shareSingleton方法获得的对象是相通的,用alloc init来构造对象的时候,得到的确实不一样的结果。究其原因,当使用alloc init创建对象时,在调用alloc方法时,oc内部会调用allocWithZone这个方法来申请内存,为了避免allocWithZone申请新的内存,可以重写allocWithZone方法,在该方法中调用shareSingleton方法返回单例对象。拷贝对象也是同样的道理。
完整代码如下:
#import "Singleton.h"
@implementation Singleton
static Singleton *shareSingleton = nil;
+ (instancetype)shareSingleton {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareSingleton = [[super allocWithZone:NULL] init];
});
return shareSingleton;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [Singleton shareSingleton];
}
- (id)copyWithZone:(struct _NSZone *)zone {
return [Singleton shareSingleton];
}
@end
测试:
Singleton *one = [Singleton shareSingleton];
NSLog(@"one = %@", one);
Singleton *two = [Singleton shareSingleton];
NSLog(@"two = %@", two);
Singleton *three = [[Singleton alloc] init];
NSLog(@"three = %@", three);
Singleton *four = [[Singleton alloc] init];
NSLog(@"four = %@", [four copy]);
Singleton *five = [Singleton new];
NSLog(@"five = %@", five);
结果:
2015-12-22 17:01:52.350 Singleton[2399:2262945] one = <Singleton: 0x7f9e6be4ae30>
2015-12-22 17:01:52.350 Singleton[2399:2262945] two = <Singleton: 0x7f9e6be4ae30>
2015-12-22 17:01:52.351 Singleton[2399:2262945] three = <Singleton: 0x7f9e6be4ae30>
2015-12-22 17:01:52.351 Singleton[2399:2262945] four = <Singleton: 0x7f9e6be4ae30>
2015-12-22 17:01:52.351 Singleton[2399:2262945] five = <Singleton: 0x7f9e6be4ae30>
转载于:https://my.oschina.net/zyboy/blog/617435
标签:Singleton,22,iOS,three,shareSingleton,two,单例,2015,写法 来源: https://blog.csdn.net/chunbo4007/article/details/100677728
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。