Objective-C中单例模式的实现
单例模式在Cocoa和Cocoa Touch中非常常见。比如这两个,[UIApplication sharedApplication]和[NSApplication sharedApplication],大家应该都见过。但是我们应该如何在代码中实现一个单例模式呢?
1.如果你对苹果的文档很熟悉的话,你一定知道,在Cocoa Foundamentals Guide中有一段实现单例模式的示例代码。大致如下
/* 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实现,中规中矩。不过这种实现并不是线程安全的。所以各路大神都各显神威,给出了多种单例模式的实现。
2.Matt Gallagher在博客中放出了一个Macro,用来实现单例模式。虽然是一个宏定义的代码,但是具体实现还是很清楚的。代码如下:
// 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; \ }
是不是感觉这两种方法很拖沓,别担心,后面将介绍简单的实现单利的方法!
3.然而,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
关于为什么上述代码就能实现单例模式,以及关于线程安全问题的考量,请参考他的博客。
4.最后介绍一个比较现代的单例模式实现。为什么说现代呢?因为这种实现利用了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; }
相关推荐
80530297 2015-04-14
shawjan 2016-01-22
fraternityjava 2020-06-14
Kele0 2020-05-30
ahnuzfm 2020-05-07
gongruitao 2020-05-02
elizabethxxy 2020-04-27
xiaoemo0 2020-04-08
付春杰Blog 2020-03-26
JF0 2020-03-20
ahnuzfm 2020-02-03
Ingram 2020-01-16
缘起宇轩阁 2019-12-30
gongruitao 2020-01-04
zuixin 2020-01-03