Objective-C中單例模式的實現

單例模式在Cocoa和Cocoa Touch中很是常見。好比這兩個,[UIApplication sharedApplication]和[NSApplication sharedApplication],你們應該都見過。可是咱們應該如何在代碼中實現一個單例模式呢? html

若是你對蘋果的文檔很熟悉的話,你必定知道,在Cocoa Foundamentals Guide中有一段實現單例模式的示例代碼。大體以下: git


/* Singleton.h */
#import <Foundation/Foundation.h>

@interface Singleton : NSObject
+ (Singleton *)instance;
@end

/* Singleton.m */
#import "Singleton.h"
static Singleton *instance = nil;

@implementation Singleton

+ (Singleton *)instance {
    if (!instance) {
        instance = [[super allocWithZone:NULL] init];
    }
    return instance;
}

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

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)init {
    if (instance) {
        return instance;
    }
    self = [super init];
    return self;
}

- (id)retain {
    return self;
}

- (oneway void)release {
    // Do nothing
}

- (id)autorelease {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;
}

@end

這是一種很標準的Singleton實現,中規中矩。不過這種實現並非線程安全的。因此各路大神都各顯神威,給出了多種單例模式的實現。 github

Matt Gallagher在博客中放出了一個Macro,用來實現單例模式。雖然是一個宏定義的代碼,可是具體實現仍是很清楚的。代碼以下: redis

//
//  SynthesizeSingleton.h
//  CocoaWithLove
//
//  Created by Matt Gallagher on 20/10/08.
//  Copyright 2009 Matt Gallagher. All rights reserved.
//
//  Permission is given to use this source code file without charge in any
//  project, commercial or otherwise, entirely at your risk, with the condition
//  that any redistribution (in part or whole) of source code must retain
//  this copyright and permission notice. Attribution in compiled projects is
//  appreciated but not required.
//

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
 \
static classname *shared##classname = nil; \
 \
+ (classname *)shared##classname \
{ \
    @synchronized(self) \
    { \
        if (shared##classname == nil) \
        { \
            shared##classname = [[self alloc] init]; \
        } \
    } \
     \
    return shared##classname; \
} \
 \
+ (id)allocWithZone:(NSZone *)zone \
{ \
    @synchronized(self) \
    { \
        if (shared##classname == nil) \
        { \
            shared##classname = [super allocWithZone:zone]; \
            return shared##classname; \
        } \
    } \
     \
    return nil; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return self; \
} \
 \
- (id)retain \
{ \
    return self; \
} \
 \
- (NSUInteger)retainCount \
{ \
    return NSUIntegerMax; \
} \
 \
- (void)release \
{ \
} \
 \
- (id)autorelease \
{ \
    return self; \
}
然而,eschaton則以爲這些實現都太繁瑣了,他給出的實現以下:

@interface SomeManager : NSObject
+ (id)sharedManager;
@end

/* 非線程安全的實現 */
@implementation SomeManager

+ (id)sharedManager {
    static id sharedManager = nil;

    if (sharedManager == nil) {
        sharedManager = [[self alloc] init];
    }

    return sharedManager;
}
@end

/* 線程安全的實現 */
@implementation SomeManager

static id sharedManager = nil;

+ (void)initialize {
    if (self == [SomeManager class]) {
        sharedManager = [[self alloc] init];
    }
}

+ (id)sharedManager {
    return sharedManager;
}
@end

關於爲何上述代碼就能實現單例模式,以及關於線程安全問題的考量,請參考他的博客objective-c

最後介紹一個比較現代的單例模式實現。爲何說現代呢?由於這種實現利用了GCD(Grand Central Dispatch)和ARC(Automatic Reference Counting)。核心代碼以下: 安全

+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = [[self alloc] init]; // or some other init method
  });
  return _sharedObject;
}

做者還寫了一個宏(gist)來方便使用,你們能夠閱讀做者的博文A note on Objective-C singletons瞭解詳情。 app

大多數狀況下,Apple官方文檔裏的單例模式的示例代碼實現已經夠用了。雖然它最繁瑣,可是也是本文介紹的幾種單例模式中最容易理解的一個。至於其餘的實現就留給讀者們根據須要選擇和應用了。 ide

(全文完) ui

相關文章
相關標籤/搜索