iOS開發-單例模式

iOS開發—單例模式 設計模式

1、簡單說明:
設計模式:多年軟件開發,總結出來的一套經驗、方法和工具
 
2、單例模式說明
(1)單例模式的做用 :能夠保證在程序運行過程,一個類只有一個實例,並且該實例易於供外界訪問,從而方便地控制了實例個數,並節約系統資源。
(2)單例模式的使用場合:在整個應用程序中,共享一份資源(這份資源只須要建立初始化1次),應該讓這個類建立出來的對象永遠只有一個。 
 
三.設計思路 
(1)永遠只分配一塊內存來建立對象
(2)提供一個類方法,返回內部惟一的一個變量
(3)最好保證init方法也只初始化一次 
 
4、 代碼示例
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

 
5、說明
重寫allocWithzone:方法,控制內存分配。由於alloc內部會調用該方法,每次調用allocWithzone:方法,系統都會建立一塊新的內存空間。
alloc方法中:永遠只分配一次內存
init方法中:保證全部的MP3數據都只加載一次。 
使用dispatch_once一次性代碼,整個程序運行過程當中,只會執行一次。默認是線程安全的
 
六、擴展
單例模式又可分爲懶漢模式和惡漢模式
上面展現的是咱們最多見的懶漢模式
另外一種模式:惡漢模式
當類加載到OC運行時環境中(內存),就會調用+(void)load一次(一個類只會加載一次)
惡漢模式是線程安全的,由於虛擬機保證只會裝載一次,在裝載類的時候是不會發生併發的。
 
 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
相關文章
相關標籤/搜索