自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Objective-C中單例模式的實(shí)現(xiàn)

移動開發(fā) iOS
單例模式是對象的創(chuàng)建模式之一,此外還包括工廠模式。單例模式的三個特點(diǎn):1、該類只有一個實(shí)例。2、該類自行創(chuàng)建該實(shí)例(在該類內(nèi)部創(chuàng)建自身的實(shí)例對象)。3、向整個系統(tǒng)公開這個實(shí)例接口。

單例模式在Cocoa和Cocoa Touch中非常常見。比如這兩個,[UIApplication sharedApplication]和[NSApplication sharedApplication],大家應(yīng)該都見過。但是我們應(yīng)該如何在代碼中實(shí)現(xiàn)一個單例模式呢?

1.如果你對蘋果的文檔很熟悉的話,你一定知道,在Cocoa Foundamentals Guide中有一段實(shí)現(xiàn)單例模式的示例代碼。大致如下

  1. /* Singleton.h */ 
  2. #import <Foundation/Foundation.h> 
  3. @interface Singleton : NSObject 
  4. + (Singleton *)instance; 
  5. @end 
  6.       
  7. /* Singleton.m */ 
  8. #import "Singleton.h" 
  9. static Singleton *instance = nil; 
  10.       
  11. @implementation Singleton 
  12. + (Singleton *)instance { 
  13. if (!instance) { 
  14.     instance = [[super allocWithZone:NULL] init]; 
  15.     return instance; 
  16. + (id)allocWithZone:(NSZone *)zone { 
  17.     return [self instance]; 
  18. - (id)copyWithZone:(NSZone *)zone { 
  19.     return self; 
  20. - (id)init { 
  21.      if (instance) { 
  22.        return instance; 
  23.     self = [super init]; 
  24.     return self; 
  25. - (id)retain { 
  26.     return self; 
  27. - (oneway void)release { 
  28.     // Do nothing 
  29. - (id)autorelease { 
  30.     return self; 
  31. - (NSUInteger)retainCount { 
  32.     return NSUIntegerMax; 
  33. @end 

這是一種很標(biāo)準(zhǔn)的Singleton實(shí)現(xiàn),中規(guī)中矩。不過這種實(shí)現(xiàn)并不是線程安全的。所以各路大神都各顯神威,給出了多種單例模式的實(shí)現(xiàn)。

2.Matt Gallagher在博客中放出了一個Macro,用來實(shí)現(xiàn)單例模式。雖然是一個宏定義的代碼,但是具體實(shí)現(xiàn)還是很清楚的。代碼如下:

  1. //  SynthesizeSingleton.h 
  2. //  CocoaWithLove 
  3. //  Created by Matt Gallagher on 20/10/08. 
  4. //  Copyright 2009 Matt Gallagher. All rights reserved. 
  5. //  Permission is given to use this source code file without charge in any 
  6. //  project, commercial or otherwise, entirely at your risk, with the condition 
  7. //  that any redistribution (in part or whole) of source code must retain 
  8. //  this copyright and permission notice. Attribution in compiled projects is 
  9. //  appreciated but not required. 
  10. // 
  11. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \ 
  12. static classname *shared##classname = nil; \ 
  13. + (classname *)shared##classname \ 
  14. { \ 
  15. @synchronized(self) \ 
  16.     { \ 
  17.          if (shared##classname == nil) \ 
  18.          { \ 
  19.              shared##classname = [[self alloc] init]; \ 
  20.           } \ 
  21.      } \ 
  22.       return shared##classname; \ 
  23. } \ 
  24. + (id)allocWithZone:(NSZone *)zone \ 
  25. { \ 
  26. @synchronized(self) \ 
  27.      { \ 
  28.         if (shared##classname == nil) \ 
  29.            { \ 
  30.               shared##classname = [super allocWithZone:zone]; \ 
  31.               return shared##classname; \ 
  32.             } \ 
  33.       } \ 
  34.     return nil; \ 
  35. } \ 
  36. - (id)copyWithZone:(NSZone *)zone \ 
  37. { \ 
  38.     return self; \ 
  39. } \ 
  40. - (id)retain \ 
  41. { \ 
  42.     return self; \ 
  43. } \ 
  44. - (NSUInteger)retainCount \ 
  45. { \ 
  46.     return NSUIntegerMax; \ 
  47. } \ 
  48. - (void)release \ 
  49. { \ 
  50. } \ 
  51. - (id)autorelease \ 
  52. { \ 
  53.     return self; \ 

是不是感覺這兩種方法很拖沓,別擔(dān)心,后面將介紹簡單的實(shí)現(xiàn)單利的方法!

#p#

3.然而,eschaton則覺得這些實(shí)現(xiàn)都太繁瑣了,他給出的實(shí)現(xiàn)如下:

  1. @interface SomeManager : NSObject 
  2. + (id)sharedManager; 
  3. @end 
  4.  
  5. /* 非線程安全的實(shí)現(xiàn) */ 
  6. @implementation SomeManager 
  7. + (id)sharedManager { 
  8. static id sharedManager = nil; 
  9. if (sharedManager == nil) { 
  10.     sharedManager = [[self alloc] init]; 
  11.     return sharedManager; 
  12. @end 
  13.  
  14. /* 線程安全的實(shí)現(xiàn) */ 
  15. @implementation SomeManager 
  16. static id sharedManager = nil; 
  17. + (void)initialize { 
  18.       if (self == [SomeManager class]) { 
  19.       sharedManager = [[self alloc] init]; 
  20.     } 
  21. + (id)sharedManager { 
  22.     return sharedManager; 
  23. @end 

關(guān)于為什么上述代碼就能實(shí)現(xiàn)單例模式,以及關(guān)于線程安全問題的考量,請參考他的博客。

4.最后介紹一個比較現(xiàn)代的單例模式實(shí)現(xiàn)。為什么說現(xiàn)代呢?因為這種實(shí)現(xiàn)利用了GCD(Grand Central Dispatch)和ARC(Automatic Reference Counting)。核心代碼如下:

  1. + (id)sharedInstance 
  2.     static dispatch_once_t pred = 0; 
  3.     __strong static id _sharedObject = nil; 
  4.     dispatch_once(&pred, ^{ 
  5.     _sharedObject = [[self alloc] init]; // or some other init method 
  6.     }); 
  7.     return _sharedObject; 

作者還寫了一個宏(gist)來方便使用,大家可以閱讀作者的博文A note on Objective-C singletons了解詳情。

大多數(shù)情況下,Apple官方文檔里的單例模式的示例代碼實(shí)現(xiàn)已經(jīng)夠用了。雖然它最繁瑣,但是也是本文介紹的幾種單例模式中最容易理解的一個。至于其他的實(shí)現(xiàn)就留給讀者們根據(jù)需要選擇和應(yīng)用了。

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2013-06-20 10:40:32

Objective-C實(shí)現(xiàn)截圖

2011-07-25 10:03:06

Objective-C 委托

2011-07-19 17:24:31

Objective-C 對象

2011-08-15 17:47:13

Objective-CisMemberOfC

2011-08-04 15:52:48

Objective-C HTML

2015-07-08 16:07:19

iOSObjective-C

2011-08-10 18:07:29

Objective-C反射

2011-08-04 10:57:33

Objective-C C語言 BOOL

2011-06-28 15:18:45

Qt 單例模式

2011-07-20 13:34:37

Objective-C self.

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2011-05-11 11:20:26

Objective-C

2011-05-11 15:58:34

Objective-C

2015-07-08 10:51:27

Objective-CRuntime

2012-01-11 09:15:45

Objective-C

2011-07-27 16:18:42

Objective-c 協(xié)議

2011-08-15 17:06:01

Objective-CNSLog

2011-07-08 18:44:09

Objective-C Self Super

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-08-02 13:16:36

Objective-C 語法 函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號