iOS開發--單例模式

 單例模式在iOS開發中可能算是最經常使用的模式之一了,可是因爲oc自己的語言特性,想要寫一個正確的單例模式相對來講比較麻煩,這裏我就拋磚引玉來聊一聊iOS中單例模式的設計思路。關於單例模式更多的介紹請參考這篇文章。html

單例顧名思義就是說一個類的實例只能有一個,在java、C++這類語言中,能夠經過將構造函數私有化來避免對象的重複建立,可是objective-c卻不可以這樣作,咱們須要經過其餘機制來達到這個目的。通常狀況下,可能咱們寫的單例模式是這樣的:java

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# import <foundation foundation.h= "" >
 
@interface Singleton : NSObject
 
+(instancetype) shareInstance ;
 
@end
 
 
 
# import "Singleton.h"
 
@implementation Singleton
 
static Singleton* _instance = nil;
 
+(instancetype) shareInstance
{
     static dispatch_once_t onceToken ;
     dispatch_once(&onceToken, ^{
         _instance = [[self alloc] init] ;
     }) ;
     
     return _instance ;
}
 
@end </foundation>

具體使用:objective-c

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# import <foundation foundation.h= "" >
# import "Singleton.h"
 
int main( int argc, const char * argv[]) {
     @autoreleasepool {
         
         Singleton* obj1 = [Singleton shareInstance] ;
         NSLog(@ "obj1 = %@." , obj1) ;
         
         Singleton* obj2 = [Singleton shareInstance] ;
         NSLog(@ "obj2 = %@." , obj2) ;
         
         //
         Singleton* obj3 = [[Singleton alloc] init] ;
         NSLog(@ "obj3 = %@." , obj3) ;
     }
     return 0 ;
}</foundation>


輸出結果爲 :函數

 

 

?
1
2
3
2014 - 12 - 15 16 : 06 : 28.344 ObjcSingleton[ 8847 : 303 ] obj1 = <singleton: 0x1001086e0 = "" >.
2014 - 12 - 15 16 : 06 : 28.346 ObjcSingleton[ 8847 : 303 ] obj2 = <singleton: 0x1001086e0 = "" >.
2014 - 12 - 15 16 : 06 : 28.346 ObjcSingleton[ 8847 : 303 ] obj3 = <singleton: 0x100103940 = "" >.</singleton:></singleton:></singleton:>

能夠看到,當咱們調用shareInstance方法時獲取到的對象是相同的,可是當咱們經過alloc和init來構造對象的時候,獲得的對象倒是不同的。spa

 

那麼問題就來了,咱們經過不一樣的途徑獲得不一樣的對象,顯然是不行的。咱們必需要確保對象的惟一性,因此咱們就須要封鎖用戶經過alloc和init以及copy來構造對象這條道路。設計

咱們知道,建立對象的步驟分爲申請內存(alloc)、初始化(init)這兩個步驟,咱們要確保對象的惟一性,所以在第一步這個階段咱們就要攔截它。當 咱們調用alloc方法時,oc內部會調用allocWithZone這個方法來申請內存,咱們覆寫這個方法,而後在這個方法中調用 shareInstance方法返回單例對象,這樣就能夠達到咱們的目的。拷貝對象也是一樣的原理,覆寫copyWithZone方法,而後在這個方法中 調用shareInstance方法返回單例對象。看代碼吧:code

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# import "Singleton.h"
 
@implementation Singleton
 
static Singleton* _instance = nil;
 
+(instancetype) shareInstance
{
     static dispatch_once_t onceToken ;
     dispatch_once(&onceToken, ^{
         _instance = [[ super allocWithZone:NULL] init] ;
     }) ;
     
     return _instance ;
}
 
+(id) allocWithZone:(struct _NSZone *)zone
{
     return [Singleton shareInstance] ;
}
 
-(id) copyWithZone:(struct _NSZone *)zone
{
     return [Singleton shareInstance] ;
}
 
@end


再看看效果如何:htm

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
main :
 
# import <foundation foundation.h= "" >
# import "Singleton.h"
 
int main( int argc, const char * argv[]) {
     @autoreleasepool {
         
         Singleton* obj1 = [Singleton shareInstance] ;
         NSLog(@ "obj1 = %@." , obj1) ;
         
         Singleton* obj2 = [Singleton shareInstance] ;
         NSLog(@ "obj2 = %@." , obj2) ;
         
         //
         Singleton* obj3 = [[Singleton alloc] init] ;
         NSLog(@ "obj3 = %@." , obj3) ;
         
         Singleton* obj4 = [[Singleton alloc] init] ;
         NSLog(@ "obj4 = %@." , [obj4 copy]) ;
     }
     return 0 ;
}</foundation>


輸出結果:對象

 

 

?
1
2
3
4
2014 - 12 - 15 16 : 11 : 24.734 ObjcSingleton[ 8979 : 303 ] obj1 = <singleton: 0x100108720 = "" >.
2014 - 12 - 15 16 : 11 : 24.735 ObjcSingleton[ 8979 : 303 ] obj2 = <singleton: 0x100108720 = "" >.
2014 - 12 - 15 16 : 11 : 24.736 ObjcSingleton[ 8979 : 303 ] obj3 = <singleton: 0x100108720 = "" >.
2014 - 12 - 15 16 : 11 : 24.736 ObjcSingleton[ 8979 : 303 ] obj4 = <singleton: 0x100108720 = "" >.</singleton:></singleton:></singleton:></singleton:>

能夠看到獲取到的對象都是同樣的了。內存

相關文章
相關標籤/搜索