一:什么是單例?
在程序的整個(gè)生命周期內(nèi)允趟,只會(huì)創(chuàng)建一個(gè)類的實(shí)例對(duì)象恼策,而且只要程序不被殺死,該實(shí)例對(duì)象就不會(huì)被釋放潮剪。
二:?jiǎn)卫饔?/h2>
1. 在應(yīng)用這個(gè)模式時(shí)涣楷,單例對(duì)象的類必須保證只有一個(gè)實(shí)例存在。許多時(shí)候整個(gè)系統(tǒng)只需要擁有一個(gè)的全局對(duì)象抗碰,這樣有利于我們協(xié)調(diào)系統(tǒng)整體的行為狮斗。比如在APP開(kāi)發(fā)中我們可能在任何地方都要使用用戶的信息,那么可以在登錄的時(shí)候就把用戶信息存放在一個(gè)文件里面弧蝇,這些配置數(shù)據(jù)由一個(gè)單例對(duì)象統(tǒng)一讀取碳褒,然后服務(wù)進(jìn)程中的其他對(duì)象再通過(guò)這個(gè)單例對(duì)象獲取這些配置信息。這種方式簡(jiǎn)化了在復(fù)雜環(huán)境下的配置管理看疗。
2. 有的情況下沙峻,某個(gè)類可能只能有一個(gè)實(shí)例。比如說(shuō)你寫了一個(gè)類用來(lái)播放音樂(lè)鹃觉,那么不管任何時(shí)候只能有一個(gè)該類的實(shí)例來(lái)播放聲音专酗。再比如,一臺(tái)計(jì)算機(jī)上可以連好幾個(gè)打印機(jī)盗扇,但是這個(gè)計(jì)算機(jī)上的打印程序只能有一個(gè)祷肯,這里就可以通過(guò)單例模式來(lái)避免兩個(gè)打印任務(wù)同時(shí)輸出到打印機(jī)中沉填,即在整個(gè)的打印過(guò)程中我只有一個(gè)打印程序的實(shí)例。
三:?jiǎn)卫齽?chuàng)建
1. GCD方式創(chuàng)建單例
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
? ? ? ?static dispatch_once_t onceToken;
? ? ? ?dispatch_once(&onceToken, ^{
? ? ? ? ? ? ? _instance = [super allocWithZone:zone];
? ? ? ?});
? ? ? ?return _instance;
}
+ (instancetype)sharedInstance
{
? ? ? ?static dispatch_once_t onceToken;
? ? ? ?dispatch_once(&onceToken, ^{
? ? ? ? ? ? ? ?_instance = [[self alloc] init];
? ? ? });
? ? ? return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
? ? ? ? ?return ? _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
? ? ? ? ? return ? _instance;
}
2. 互斥鎖方式
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
? ? ? ? @synchronized(self) {
? ? ? ? ? ? ? ? if (_instance == nil) {
? ? ? ? ? ? ? ? _instance = [super allocWithZone:zone];
? ? ? ? }
}
? ? ? ? ? return _instance;
}
+ (instancetype)sharedInstance
{
? ? ? ? ? @synchronized(self) {
? ? ? ? ? ?if (_instance == nil) {
? ? ? ? ? ? ? ? _instance = [[self alloc] init];
? ? ? ? ? ? }
}
? ? ? ? ? return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
? ? ? ? ? return _instance;
}
注意:互斥鎖會(huì)影響性能佑笋,所以最好還是使用GCD方式創(chuàng)建單例翼闹。
四:宏創(chuàng)建單例
Singleton.h文件
==================================
#define SingletonH(name) + (instancetype)shared##name;
#define SingletonM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}\
\
- (id)mutableCopyWithZone:(NSZone *)zone { \
return _instance; \
}
調(diào)用方式是:
viewcontroller.h文件
===========================
#import
#import?"Singleton.h"
@interface?ViewController?:?UIViewController
SingletonH(viewController)
@end
viewcontroller.m文件
===========================
@interface?ViewController?()
@end
@implementation?ViewController
SingletonM(ViewController)
-?(void)viewDidLoad?{
[super?viewDidLoad];
NSLog(@"%@?%@?%@?%@",?[ViewController?sharedViewController],[ViewController?sharedViewController],?[[ViewController?alloc]?init],[[ViewController?alloc]?init]);
}
@end