iOS開發—單例模式 設計模式
1 #import <Foundation/Foundation.h> 2 3 @interface YXAudioTool : NSObject 4 5 +(instancetype)shareAudioTool; 6 7 @end
1 #import "YXAudioTool.h" 2 3 @interface YXAudioTool() 4 5 //@property (nonatomic,strong) NSMutableDictionary * muscis; 6 7 @end 8 9 @implementation YXAudioTool 10 11 static id _instance; 12 13 - (instancetype)init 14 { 15 static dispatch_once_t onceToken; 16 dispatch_once(&onceToken, ^{ 17 if ((self == [super init])) { 18 //加載所需音樂資源 19 } 20 }); 21 return self; 22 } 23 24 +(instancetype)allocWithZone:(struct _NSZone *)zone{ 25 static dispatch_once_t onceToken; 26 dispatch_once(&onceToken, ^{ 27 _instance = [super allocWithZone:zone]; 28 }); 29 return _instance; 30 } 31 32 +(instancetype)shareAudioTool{ 33 static dispatch_once_t onceToken; 34 dispatch_once(&onceToken, ^{ 35 _instance = [[self alloc]init]; 36 }); 37 return _instance; 38 } 39 40 +(instancetype)copyWithZone:(struct _NSZone *)zone{ 41 return _instance; 42 } 43 44 +(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 45 return _instance; 46 } 47 48 -(instancetype)copyWithZone:(struct _NSZone *)zone{ 49 return _instance; 50 } 51 52 -(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 53 return _instance; 54 } 55 56 @end
1 #import "YXAudioTool.h" 2 3 @interface YXAudioTool() 4 5 //@property (nonatomic,strong) NSMutableDictionary * muscis; 6 7 @end 8 9 @implementation YXAudioTool 10 11 static id _instance; 12 13 - (instancetype)init 14 { 15 static dispatch_once_t onceToken; 16 dispatch_once(&onceToken, ^{ 17 if ((self == [super init])) { 18 //加載所需音樂資源 19 } 20 }); 21 return self; 22 } 23 24 +(instancetype)allocWithZone:(struct _NSZone *)zone{ 25 _instance = [super allocWithZone:zone]; 26 return _instance; 27 } 28 29 +(void)load{ 30 _instance = [[self alloc]init]; 31 } 32 33 +(instancetype)shareAudioTool{ 34 return _instance; 35 } 36 37 +(instancetype)copyWithZone:(struct _NSZone *)zone{ 38 return _instance; 39 } 40 41 +(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 42 return _instance; 43 } 44 45 -(instancetype)copyWithZone:(struct _NSZone *)zone{ 46 return _instance; 47 } 48 49 -(instancetype)mutableCopyWithZone:(struct _NSZone *)zone{ 50 return _instance; 51 } 52 53 @end