iOS設計模式之單例模式


單例模式是iOS常用設計模式中的一種。單例設計模式的作用是使得這個類的一個對象成為系統中的唯一實例,因此需要用一種唯一的方法去創建這個對象並返回這個對象的地址。那么,我們何時使用單例模式呢?1、類只能有一個實例,而且必須從一個為人熟知的訪問點對其訪問。2、這個唯一的實例只能通過子類化進行擴展,而且擴展的對象不會破壞客戶端代碼。

那么用Objective-C如何實現單例模式呢?下面我們來新建一個Singleton類,在Singleton.h中實現如下

 

  1. @interface Singleton : NSObject  
  2.   
  3. + (Singleton *) sharedInstance;  
  4.   
  5. @end  

 

 

在Singleton.m

 
  1. @implementation Singleton  
  2.   
  3. static Singleton * sharedSingleton = nil;  
  4.   
  5. + (Singleton *) sharedInstance  
  6. {  
  7.     if (sharedSingleton == nil) {  
  8.         sharedSingleton = [[Singleton alloc] init];  
  9.     }  
  10.     return sharedSingleton;  
  11. }  
  12.   
  13. @end  


這樣就創建一個簡單的單例模式,實際上有一部分程序員也是這樣實現的,但實際上這是一個不“嚴格”版本,在實際中使用,可能會遇到發起調用的對象不能以其他分配方式實例化單例對象,否則,就會創建多個實例。(之前有人和我討論過這個問題,說使用者應該嚴格按照接口來使用,當實際上Singleton是一個對象,我們不能保證使用者不會使用其他的方法去創建(比如alloc),這個時候他就會創建多個實例,這樣就會出現這些無法感知的bug)

 

 

下面我對Singleton.m的進行改進


  1. @implementation Singleton  
  2.   
  3. static Singleton * sharedSingleton = nil;  
  4.   
  5. + (Singleton *) sharedInstance  
  6. {  
  7.     if (sharedSingleton == nil) {  
  8.         sharedSingleton = [[super allocWithZone:NULL] init];  
  9.     }  
  10.     return sharedSingleton;  
  11. }  
  12.   
  13. + (id) allocWithZone:(struct _NSZone *)zone  
  14. {  
  15.     return [[self sharedInstance] retain];  
  16. }  
  17.   
  18. - (id) copyWithZone:(NSZone *) zone  
  19. {  
  20.     return self;  
  21. }  
  22.   
  23. - (idretain  
  24. {  
  25.     return self;  
  26. }  
  27.   
  28. - (NSUInteger) retainCount  
  29. {  
  30.     return NSUIntegerMax;  
  31. }  
  32.   
  33.   
  34. - (void) release  
  35. {  
  36.     //  
  37. }  
  38.   
  39. - (id) autorelease  
  40. {  
  41.     return self;  
  42. }  
  43.   
  44. @end  


也許你注意到了,我重載了allocWithZone:,保持了從sharedInstance方法返回的單例對象,使用者哪怕使用alloc時也會返回唯一的實例(alloc方法中會先調用allocWithZone:創建對象)。而retain等內存管理的函數也被重載了,這樣做讓我們有了把Singleton類變得“嚴格”了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM