iOS設計模式——單例模式

單例模式用於當一個類只能有一個實例的時候, 通常情況下這個單例代表的是某一個物理設備比如打印機,或是某種不可以有多個實例同時存在的虛擬資源或是系統屬性比如一個程序的某個引擎或是數據。用單例模式加以控制是非常有必要的。

單例模式需要達到的目的

1. 封裝一個共享的資源

2. 提供一個固定的實例創建方法

3. 提供一個標準的實例訪問接口

單例模式的創建

本文以創建一個MySingletonClass的單例模式爲例。首先,我們需要定義一個類MySingletonClass.

@interface MySingletonClass:NSObject {
    
}

並且爲其添加一個類方法(注意,這裏不是實例方法)+(id)sharedInstance;一個基本的實現寫法如下:

static MySingletonClass *sharedCLDelegate = nil;
+(MySingletonClass *)sharedInstance{
    @synchronized(self) {
        if(sharedCLDelegate == nil) {
            [[[self class] alloc] init]; //   assignment   not   done   here
        }
    }
    return sharedCLDelegate;
}

在上面的代碼中(用到了關鍵字@synchronized是爲了保證我們的單例的線程級別的安全,可以適用於多線程模式下。)static變量sharedCLDelegate用於存儲一個單例的指針,並且強制所有對該變量的訪問都必須通過類方法   +(id)sharedInstance,在對   +(id)sharedInstance第一次調用時候完成實例的創建。這裏值得留意一下的是,上面代碼中用的是[[selfclass] alloc],而不是 [MySingletonClass alloc],一般情況下這兩種寫法產生同樣的效果,但是這裏這樣做是爲了更好的利用OOP的性質,[selfclass]可以動態查找並確定類的類型從而便於實現對該類的子類化。

對實例化的控制

爲了完全的實現實例的單態性,必須通過一定手段來避免實例多次被創建。+(id)sharedInstance控制了單例的創建和訪問,但是並不能控制其它地方的代碼通過alloc方法來創建更多的實例,因此我們還要重載任何一個涉及到allocation的方法,這些方法包括   +new, +alloc,+allocWithZone:, -copyWithZone:, 以及 -mutableCopyWithZone: 另外,+(id)sharedInstance也需要稍作修改。

+ (id)hiddenAlloc
{
    return [super alloc];
}


+ (id)alloc
{
    NSLog(@"%@: use +sharedInstance instead of +alloc", [[self class] name]);
    return nil;
}


+ (id)new
{
    return [self alloc];
}

+(id)allocWithZone:(NSZone*)zone
{
    return [self alloc];
}

-   (id)copyWithZone:(NSZone *)zone
{   // -copy inherited from NSObject calls -copyWithZone:
    NSLog(@"MySingletonClass: attempt to -copy may be a bug.");
    [self retain];
    return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
    // -mutableCopy inherited from NSObject calls -mutableCopyWithZone:
    return [self copyWithZone:zone];
}

+(id)sharedInstance修改如下:

+ (MySingletonClass *)sharedInstance {
    @synchronized(self) {
        if (sharedCLDelegate == nil)   {
            [[[self class] hiddenAlloc] init]; // assignment not done here
        }
    }
    return sharedCLDelegate;
}

如果不考慮類的子類化,+hiddenAlloc這個方法可以省略。由於我們是用[selfclass]來實現類型的動態識別,用[[selfclass] hiddenAlloc]可以避免調用到被重載過的alloc方法。此外,hiddenAlloc也爲可能的子類化提供了一個調用原始alloc方法的機會。上面重載過的alloc方法只是給出一個log信息並且返回nilCopying方法裏只是簡單的增加了retain的計數並沒有返回一個新的實例。這也正體現了單例模式的性質,因爲技術上來講,拷貝一個單例是錯誤的(因爲是單例)所以在copyWithZone方法中我們給出了一個錯誤信息,當然也可以扔出一個exception

單例的銷燬

通常我們在   -(void)applicationWillTerminate:(UIApplication *)application方法中調用如下方法:

+ (void)attemptDealloc
{
    if ([sharedCLDelegate retainCount] != 1)
        return;

    [sharedCLDelegate release];
    myInstance = nil;
}

值得注意的是,上面這個attemptDealloc方法顧名思義,只是試圖釋放掉這個單例。如果retain的計數不爲1,說明還有其他地方對該單例發送過retain消息。考慮到一個單例模式的生存週期是整個程序結束爲止。所以,在程序的任何一個地方都沒有必要向這個單例發送retain消息,即便是對這個單例有引用。而是調用sharedInstance方法來引用這個單例,這樣做是安全的,也是合乎單例模式的技術含義的。

iOS中的單例模式應用

iOS中好幾個類都是採用了單例模式,比如NSApplication NSFontManager,   NSDocumentController,NSHelpManager, NSNull,NSProcessInfo, NSScriptExecutionContext,   NSUserDefaults.


如果本文有任何錯誤之處,歡迎拍磚指正,共同進步, 謝謝!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章