Objective-C中單例模式的實(shí)現(xiàn)
單例模式在Cocoa和Cocoa Touch中非常常見。比如這兩個,[UIApplication sharedApplication]和[NSApplication sharedApplication],大家應(yīng)該都見過。但是我們應(yīng)該如何在代碼中實(shí)現(xiàn)一個單例模式呢?
1.如果你對蘋果的文檔很熟悉的話,你一定知道,在Cocoa Foundamentals Guide中有一段實(shí)現(xiàn)單例模式的示例代碼。大致如下
- /* 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
這是一種很標(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)還是很清楚的。代碼如下:
- // 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; \
- }
是不是感覺這兩種方法很拖沓,別擔(dān)心,后面將介紹簡單的實(shí)現(xiàn)單利的方法!
#p#
3.然而,eschaton則覺得這些實(shí)現(xiàn)都太繁瑣了,他給出的實(shí)現(xiàn)如下:
- @interface SomeManager : NSObject
- + (id)sharedManager;
- @end
- /* 非線程安全的實(shí)現(xiàn) */
- @implementation SomeManager
- + (id)sharedManager {
- static id sharedManager = nil;
- if (sharedManager == nil) {
- sharedManager = [[self alloc] init];
- }
- return sharedManager;
- }
- @end
- /* 線程安全的實(shí)現(xiàn) */
- @implementation SomeManager
- static id sharedManager = nil;
- + (void)initialize {
- if (self == [SomeManager class]) {
- sharedManager = [[self alloc] init];
- }
- }
- + (id)sharedManager {
- return sharedManager;
- }
- @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)。核心代碼如下:
- + (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了解詳情。
大多數(shù)情況下,Apple官方文檔里的單例模式的示例代碼實(shí)現(xiàn)已經(jīng)夠用了。雖然它最繁瑣,但是也是本文介紹的幾種單例模式中最容易理解的一個。至于其他的實(shí)現(xiàn)就留給讀者們根據(jù)需要選擇和應(yīng)用了。