单例模式
1 // 2 // BWMusicTool.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWMusicTool : NSObject<NSCopying> 12 13 //@property (nonatomic, strong) NSMutableArray *musics; 14 15 + (instancetype)sharedMusicTool; 16 @end 17 18 19 // 20 // BWMusicTool.m 21 // IOS_0118_hehe 22 // 23 // Created by ma c on 16/1/18. 24 // Copyright (c) 2016年 博文科技. All rights reserved. 25 // 懒汉式 26 27 #import "BWMusicTool.h" 28 29 @interface BWMusicTool () 30 31 @end 32 33 34 @implementation BWMusicTool 35 36 static id _instance; 37 38 39 //- (NSArray *)musics 40 //{ 41 // if (!_musics) { 42 // _musics = [NSMutableArray array]; 43 // //加载歌曲 44 // } 45 // return _musics; 46 //} 47 48 //+ (instancetype)alloc 49 //{ 50 // 51 //} 52 53 //alloc方法内部会调用这个方法 54 + (instancetype)allocWithZone:(struct _NSZone *)zone 55 { 56 // NSLog(@"allocWithZone"); 57 58 if (_instance == nil) {//防止频繁枷锁 59 60 @synchronized(self){ 61 62 if (_instance == nil) {//防止创建多次 63 _instance = [super allocWithZone:zone]; 64 } 65 } 66 } return _instance; 67 } 68 69 + (instancetype)sharedMusicTool 70 { 71 if (_instance == nil) {//防止频繁枷锁 72 73 @synchronized(self){ 74 if (_instance == nil) {//防止创建多次 75 76 _instance = [[BWMusicTool alloc] init]; 77 } 78 } 79 } 80 //return [[self alloc] init]; 81 return _instance; 82 } 83 84 - (id)copyWithZone:(NSZone *)zone 85 { 86 return _instance; 87 } 88 89 @end
1 // 2 // BWSoundTool.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWSoundTool : NSObject 12 13 + (instancetype)sharedSoundTool; 14 15 @end 16 17 // 18 // BWSoundTool.m 19 // IOS_0118_hehe 20 // 21 // Created by ma c on 16/1/18. 22 // Copyright (c) 2016年 博文科技. All rights reserved. 23 // 饿汉式 24 25 #import "BWSoundTool.h" 26 27 @implementation BWSoundTool 28 29 static id _instance; 30 31 //当类加载到OC运行时环境中(内存),就会调用一次(一个类只会加载一次) 32 + (void)load 33 { 34 // NSLog(@"load1 ----"); 35 _instance = [[self alloc] init]; 36 // NSLog(@"load2 ---- %@",_instance); 37 } 38 39 /* 40 //第一次使用这个类时才会调用 41 + (void)initialize 42 { 43 NSLog(@"initialize"); 44 } 45 */ 46 47 //alloc方法内部会调用这个方法 48 + (instancetype)allocWithZone:(struct _NSZone *)zone 49 { 50 // NSLog(@"allocWithZone"); 51 52 if (_instance == nil) {//防止创建多次 53 _instance = [super allocWithZone:zone]; 54 } 55 56 return _instance; 57 } 58 59 + (instancetype)sharedSoundTool 60 { 61 return _instance; 62 } 63 64 - (id)copyWithZone:(NSZone *)zone 65 { 66 return _instance; 67 } 68 69 70 @end
1 // 2 // BWDataModel.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWDataModel : NSObject<NSCopying> 12 13 + (instancetype)sharedDataTool; 14 15 @end 16 17 18 // 19 // BWDataModel.m 20 // IOS_0118_hehe 21 // 22 // Created by ma c on 16/1/18. 23 // Copyright (c) 2016年 博文科技. All rights reserved. 24 // 25 26 #import "BWDataModel.h" 27 28 @implementation BWDataModel 29 //用来保存唯一的单例对象 30 static id _instance; 31 32 + (instancetype)allocWithZone:(struct _NSZone *)zone 33 { 34 static dispatch_once_t onceToken; 35 dispatch_once(&onceToken, ^{ 36 _instance = [super allocWithZone:zone]; 37 }); 38 return _instance; 39 } 40 + (instancetype)sharedDataTool 41 { 42 static dispatch_once_t onceToken; 43 dispatch_once(&onceToken, ^{ 44 _instance = [[BWDataModel alloc] init]; 45 }); 46 return _instance; 47 } 48 49 - (id)copyWithZone:(NSZone *)zone 50 { 51 return _instance; 52 } 53 54 @end
1 // 2 // ViewController.m 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWMusicTool.h" 11 #import "BWSoundTool.h" 12 #import "BWDataModel.h" 13 14 @interface ViewController () 15 16 @end 17 18 //单例模式可以保证运行过程中,一个类只有一个实例(对象) 19 @implementation ViewController 20 /* 21 static:修饰变量 22 1>修饰全局变量:全局变量的作用域仅限于当前文件内部; 23 不用static,在当前文件外部也可以访问(extern id _musicTool;) 24 25 2>修饰局部变量:能保证局部变量永远只初始化一次,在运行过程中,永远只有一份 26 */ 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 self.view.backgroundColor = [UIColor cyanColor]; 30 31 /***************************懒汉式*********************************/ 32 BWMusicTool *musicTool1 = [[BWMusicTool alloc] init]; 33 BWMusicTool *musicTool2 = [BWMusicTool sharedMusicTool]; 34 // copy 有可能产生新的对象 35 BWMusicTool *musicTool3 = [musicTool2 copy]; 36 NSLog(@" BWMusicTool:%@ -- %@ -- %@",musicTool1,musicTool2,musicTool3); 37 //引用全局变量(并非定义) 38 // extern id _musicTool; 39 // NSLog(@"%@",_musicTool); 40 41 /***************************饿汉式*********************************/ 42 BWSoundTool *soundTool1 = [[BWSoundTool alloc] init]; 43 BWSoundTool *soundTool2 = [BWSoundTool sharedSoundTool]; 44 NSLog(@" BWSoundTool:%@ -- %@",soundTool1,soundTool2); 45 46 /***************************GCD*********************************/ 47 BWDataModel *dataModel1 = [[BWDataModel alloc] init]; 48 BWDataModel *dataModel2 = [BWDataModel sharedDataTool]; 49 NSLog(@" BWDataModel:%@ -- %@",dataModel1,dataModel2); 50 51 } 52 53 - (void)didReceiveMemoryWarning { 54 [super didReceiveMemoryWarning]; 55 // Dispose of any resources that can be recreated. 56 } 57 58 @end
非ARC情况下-保证单例不被释放
1 // 2 // BWDataModel.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "BWSingleton.h" 11 @interface BWDataModel : NSObject<NSCopying> 12 13 + (instancetype)sharedInstance; 14 @end 15 16 17 // 18 // BWDataModel.m 19 // IOS_0118_hehe 20 // 21 // Created by ma c on 16/1/18. 22 // Copyright (c) 2016年 博文科技. All rights reserved. 23 // 24 25 #import "BWDataModel.h" 26 27 @implementation BWDataModel 28 29 //用来保存唯一的单例对象 30 static id _instance; 31 32 + (instancetype)allocWithZone:(struct _NSZone *)zone 33 { 34 static dispatch_once_t onceToken; 35 dispatch_once(&onceToken, ^{ 36 _instance = [super allocWithZone:zone]; 37 }); 38 return _instance; 39 } 40 + (instancetype)sharedInstance 41 { 42 static dispatch_once_t onceToken; 43 dispatch_once(&onceToken, ^{ 44 _instance = [[BWDataModel alloc] init]; 45 }); 46 return _instance; 47 } 48 49 - (id)copyWithZone:(NSZone *)zone 50 { 51 return _instance; 52 } 53 54 - (oneway void)release 55 { 56 57 } 58 59 - (instancetype)retain 60 { 61 return self; 62 } 63 64 - (NSUInteger)retainCount 65 { 66 return 1; 67 } 68 69 - (instancetype)autorelease 70 { 71 return self; 72 } 73 74 @end 75 76 77 78 // 79 // ViewController.m 80 // IOS_0118_singleton 81 // 82 // Created by ma c on 16/1/18. 83 // Copyright (c) 2016年 博文科技. All rights reserved. 84 // 85 86 #import "ViewController.h" 87 #import "BWDataModel.h" 88 89 @interface ViewController () 90 91 @end 92 93 @implementation ViewController 94 95 - (void)viewDidLoad { 96 [super viewDidLoad]; 97 98 BWDataModel *dataModel1 = [[BWDataModel alloc] init]; 99 BWDataModel *dataModel2 = [BWDataModel sharedInstance]; 100 NSLog(@" %@---------%@",dataModel1,dataModel2); 101 102 } 103 104 - (void)didReceiveMemoryWarning { 105 [super didReceiveMemoryWarning]; 106 // Dispose of any resources that can be recreated. 107 } 108 109 @end
宏定义单例模式
1 // .h文件 2 #define BWSingletonH + (instancetype)sharedInstance; 3 4 // .m文件 5 #define BWSingletonM 6 7 static id _instance; 8 9 + (instancetype)allocWithZone:(struct _NSZone *)zone 10 { 11 static dispatch_once_t onceToken; 12 dispatch_once(&onceToken, ^{ 13 _instance = [super allocWithZone:zone]; 14 }); 15 return _instance; 16 } 17 18 + (instancetype)sharedInstance 19 { 20 static dispatch_once_t onceToken; 21 dispatch_once(&onceToken, ^{ 22 _instance = [[self alloc] init]; 23 }); 24 return _instance; 25 } 26 27 - (id)copyWithZone:(NSZone *)zone 28 { 29 return _instance; 30 }
1 // 2 // BWDataModel.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "BWSingleton.h" 11 @interface BWDataModel : NSObject<NSCopying> 12 13 BWSingletonH 14 15 @end 16 17 18 // 19 // BWDataModel.m 20 // IOS_0118_hehe 21 // 22 // Created by ma c on 16/1/18. 23 // Copyright (c) 2016年 博文科技. All rights reserved. 24 // 25 26 #import "BWDataModel.h" 27 28 @implementation BWDataModel 29 30 BWSingletonM 31 32 @end 33 34 35 @end
1 // 2 // ViewController.m 3 // IOS_0118_singleton 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWDataModel.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 21 BWDataModel *dataModel1 = [[BWDataModel alloc] init]; 22 BWDataModel *dataModel2 = [BWDataModel sharedInstance]; 23 NSLog(@" %@---------%@",dataModel1,dataModel2); 24 25 } 26 27 - (void)didReceiveMemoryWarning { 28 [super didReceiveMemoryWarning]; 29 // Dispose of any resources that can be recreated. 30 } 31 32 @end
宏定义单例模式(更改方法名及适配MRC和ARC)
BWSingleton.h
1 // .h文件 2 #define BWSingletonH(name) + (instancetype)shared##name; 3 4 5 // .m文件 6 #if __has_feature(objc_arc) 7 8 #define BWSingletonM(name) 9 10 static id _instance; 11 12 + (instancetype)allocWithZone:(struct _NSZone *)zone 13 { 14 static dispatch_once_t onceToken; 15 dispatch_once(&onceToken, ^{ 16 _instance = [super allocWithZone:zone]; 17 }); 18 return _instance; 19 } 20 21 + (instancetype)shared##name 22 { 23 static dispatch_once_t onceToken; 24 dispatch_once(&onceToken, ^{ 25 _instance = [[self alloc] init]; 26 }); 27 return _instance; 28 } 29 30 - (id)copyWithZone:(NSZone *)zone 31 { 32 return _instance; 33 } 34 35 #else 36 37 #define BWSingletonM(name) 38 39 static id _instance; 40 41 + (instancetype)allocWithZone:(struct _NSZone *)zone 42 { 43 static dispatch_once_t onceToken; 44 dispatch_once(&onceToken, ^{ 45 _instance = [super allocWithZone:zone]; 46 }); 47 return _instance; 48 } 49 + (instancetype)shared##name 50 { 51 static dispatch_once_t onceToken; 52 dispatch_once(&onceToken, ^{ 53 _instance = [[self alloc] init]; 54 }); 55 return _instance; 56 } 57 58 - (id)copyWithZone:(NSZone *)zone 59 { 60 return _instance; 61 } 62 63 - (oneway void)release 64 { 65 66 } 67 68 - (instancetype)retain 69 { 70 return self; 71 } 72 73 - (NSUInteger)retainCount 74 { 75 return 1; 76 } 77 78 - (instancetype)autorelease 79 { 80 return self; 81 } 82 83 #endif
1 // 2 // BWDataModel.h 3 // IOS_0118_hehe 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "BWSingleton.h" 11 @interface BWDataModel : NSObject 12 13 BWSingletonH(dataModel) 14 15 @end 16 17 // 18 // BWDataModel.m 19 // IOS_0118_hehe 20 // 21 // Created by ma c on 16/1/18. 22 // Copyright (c) 2016年 博文科技. All rights reserved. 23 // 24 25 #import "BWDataModel.h" 26 27 @implementation BWDataModel 28 29 BWSingletonM(dataModel) 30 31 @end
1 // 2 // ViewController.m 3 // IOS_0118_singleton 4 // 5 // Created by ma c on 16/1/18. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWDataModel.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 21 BWDataModel *dataModel1 = [[BWDataModel alloc] init]; 22 BWDataModel *dataModel2 = [BWDataModel shareddataModel]; 23 NSLog(@" %@---------%@",dataModel1,dataModel2); 24 25 #if __has_feature(objc_arc) 26 //编译器环境是ARC 27 28 #else 29 //编译器环境是MRC 30 [dataModel2 release]; 31 [dataModel1 release]; 32 #endif 33 34 } 35 36 @end