單例設計模式的基本步驟:
- 聲明一個單件對象的靜態(tài)實例,并初始化為
nil。- 創(chuàng)建一個類的類工廠方法,當且僅當這個類的實例為
nil時生成一個該類的實例- 實現(xiàn)
NScopying協(xié)議, 覆蓋allocWithZone:方法,確保用戶在直接分配和初始化對象時,不會產(chǎn) 生另一個對象。- 覆蓋
release、autorelease、retain、retainCount方法, 以此確保單例的狀態(tài)。- 在多線程的環(huán)境中,注意使用
@synchronized關鍵字或GCD,確保靜態(tài)實例被正確的創(chuàng)建和初始化。
MRC寫法
#import "SoundTool.h"
static SoundTool * _instance = nil;
@implementation SoundTool
/**
* alloc方法內(nèi)部會調(diào)用allocWithZone
* zone:系統(tǒng)分配給app的內(nèi)存
*/
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (instancetype)init{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
});
return _instance;
}
+ (instancetype)shareSoundTool{
return [[self alloc] init];
}
- (oneway void)release{
}
- (instancetype)retain{
return self;
}
- (NSUInteger)retainCount{
return 1;
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return _instance;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return _instance;
}
@end
ARC寫法
#import "SoundTool.h"
static SoundTool * _instance = nil;
@implementation SoundTool
/**
* alloc方法內(nèi)部會調(diào)用allocWithZone
* zone:系統(tǒng)分配給app的內(nèi)存
*/
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (instancetype)init{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
});
return _instance;
}
+ (instancetype)shareSoundTool{
return [[self alloc] init];
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return _instance;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return _instance;
}
@end
若需要創(chuàng)建多個單例可將其寫成宏
使用以下語法來區(qū)分
ARC與MRC#if __has_feature(objc_arc) //ARC #else //MRC #endif
代碼如下:
//
// Singleton.h
// LantaiyuanBus
//
// Created by lantaiyuan on 16/9/7.
// Copyright ? 2016年 youmy. All rights reserved.
//
// .h文件的實現(xiàn)
#define SingletonH(methodName) + (instancetype)shared##methodName;
// .m文件的實現(xiàn)
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#else // 不是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#endif
用法:
.h 文件
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface VideoTool : NSObject
SingletonH(VideoTool)
@end
.m 文件
#import "VideoTool.h"
@implementation VideoTool
SingletonM(VideoTool)
@end