整理单例设计模式
先上类图:
再上一个自己用MindNode整理的内容:
下边是整理的代码
1 #import <Foundation/Foundation.h> 2 3 4 /** 5 WW单例设计模式 6 */ 7 @interface WWSingletonPattern : NSObject 8 9 /** 10 单例 11 12 @return 返回单例 13 */ 14 + (WWSingletonPattern *)sharedInstance; 15 @end
1 #import "WWSingletonPattern.h" 2 3 @implementation WWSingletonPattern 4 5 6 static WWSingletonPattern *sharedInstance = nil; 7 #if __has_feature(objc_arc) 8 //arc 环境下 9 //#pragma mark - 加锁的方式来创建单例 10 //+ (WWSingletonPattern *)sharedInstance{ 11 // static WWSingletonPattern *sharedInstance = nil; 12 // @synchronized(self){ 13 // if (!sharedInstance) { 14 // sharedInstance = [[self alloc]init]; 15 // } 16 // } 17 // return sharedInstance; 18 //} 19 20 //#pragma mark - GCD的方式创建单例 21 //+ (WWSingletonPattern *)sharedInstance{ 22 // static WWSingletonPattern *sharedInstance = nil; 23 // static dispatch_once_t token; 24 // dispatch_once(&token, ^{ 25 // sharedInstance = [[self alloc]init]; 26 // }); 27 // return sharedInstance; 28 //} 29 30 //#pragma mark - 单例 31 + (WWSingletonPattern *)sharedInstance{ 32 return [[self alloc]init]; 33 } 34 35 - (instancetype)init{ 36 if (self = [super init]) { 37 38 } 39 return self; 40 } 41 42 #pragma mark - GCD的方式创建单例 43 + (instancetype)allocWithZone:(struct _NSZone *)zone{ 44 //执行 alloc init后会调用allocWithZone;而new相当于alloc init的组合体 45 if (!sharedInstance) { 46 dispatch_once_t token = 0; 47 dispatch_once(&token, ^{ 48 sharedInstance = [super allocWithZone:zone]; 49 }); 50 } 51 return sharedInstance; 52 } 53 - (id)copy{ 54 return self; 55 } 56 - (id)mutableCopy{ 57 return self; 58 } 59 #else 60 //MRC 环境下 61 //#pragma mark - 单例 62 + (WWSingletonPattern *)sharedInstance{ 63 return [[self alloc]init]; 64 } 65 66 #pragma mark - GCD的方式创建单例 67 + (instancetype)allocWithZone:(struct _NSZone *)zone{ 68 //执行 alloc init后会调用allocWithZone;而new相当于alloc init的组合体 69 if (!sharedInstance) { 70 dispatch_once_t token = 0; 71 dispatch_once(&token, ^{ 72 sharedInstance = [super allocWithZone:zone]; 73 }); 74 } 75 return sharedInstance; 76 } 77 - (id)copy{ 78 return self; 79 } 80 - (id)mutableCopy{ 81 return self; 82 } 83 84 - (instancetype)retain{ 85 return self; 86 } 87 88 - (NSUInteger)retainCount{ 89 //表示不能释放的对象 90 return NSUIntegerMax; 91 } 92 93 - (oneway void)release{ 94 //什么也不做 95 } 96 97 - (instancetype)autorelease{ 98 return self; 99 } 100 101 - (void)dealloc{ 102 [super dealloc]; 103 } 104 105 #endif 106 107 @end
参考书籍:设计模式之禅(109-120) Effective-Objective-C 2.0编写高质量iOS于OS X代码的52个有效方法(第45条) Objective-C编程之道(80-87)
参考网址:
https://zonble.gitbooks.io/kkbox-ios-dev/design_patterns/singleton.html
http://www.runoob.com/design-pattern/singleton-pattern.html
http://www.galloway.me.uk/tutorials/singleton-classes/
http://ibloodline.com/articles/2016/09/19/singleton.html
https://www.cnblogs.com/JackieHoo/p/5050010.html
http://blog.csdn.net/sqc3375177/article/details/11836521
http://blog.csdn.net/smallyou113/article/details/51892915
http://www.jianshu.com/p/0607505ed2e8
扩展:
http://blog.sunnyxx.com/2015/06/12/objc-new-features-in-2015/
https://stackoverflow.com/questions/33326713/what-is-the-motivation-for-querying-has-featureobjc-arc
iOS交流群欢饮你的加入!
群二维码:
先写到这么多,以后再更新;
如有问题,敬请指正;
如需转载,请注明出处,谢谢!