單例有兩種模式,一種是餓漢式
,一種是懶漢式
.所謂懶漢式
即使用時(shí)創(chuàng)建對(duì)象榜轿,類似于懶加載或粮。餓漢式
即為當(dāng)程序運(yùn)行加載到內(nèi)存時(shí)就創(chuàng)建导饲。
> 懶漢式的創(chuàng)建方式
+ (void)initialize {
if (_shareIntance == nil) {
NSLog(@"load---我被創(chuàng)建了");
_shareIntance = [[self alloc] init];
}
}
程序一運(yùn)行就會(huì)創(chuàng)建
> 餓漢式的創(chuàng)建方式
#import "ViewController.h"
#import "ZQMusicManager.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ZQMusicManager *m = [[ZQMusicManager alloc] init];
}
ZQMusicManager.m中
+ (void)initialize {
if (_shareIntance == nil) {
NSLog(@"initialize---我被創(chuàng)建了");
_shareIntance = [[self alloc] init];
}
}
運(yùn)行結(jié)果
常用gcd方式來實(shí)現(xiàn)單例,完整形式如下
static id _shareIntance;
+ (instancetype)shareIntance
{
if (_shareIntance == nil) {
_shareIntance = [[self alloc] init];
}
return _shareIntance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!_shareIntance) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shareIntance = [super allocWithZone:zone];
});
}
return _shareIntance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _shareIntance;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return _shareIntance;
}
注意:單例不可繼承
項(xiàng)目里有很多單例時(shí)氯材,可使用宏創(chuàng)建
// .h文件
#define HMSingletonH + (instancetype)sharedInstance;
// .m文件
#define HMSingletonM \
static id _instance; \
\
+ (id)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; \
}
.h
直接使用宏
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ZQMusicManager : NSObject
ZQSingletonH
@end
NS_ASSUME_NONNULL_END
.m
直接使用宏
#import "ZQMusicManager.h"
@implementation ZQMusicManager
ZQSingletonM
@end
控制器
里可以直接使用
- (void)viewDidLoad {
[super viewDidLoad];
ZQMusicManager *m = [ZQMusicManager sharedInstance];
}
如果想像[UIApplication sharedApplication]
可以寫sharedXXX
宏可以如下定義:
#define ZQSingletonH(name) + (instancetype)shared##name;
// .m文件
#define ZQSingletonM(name) \
static id _instance; \
\
+ (id)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; \
}
.h
使用方式
@interface ZQMusicManager : NSObject
ZQSingletonH(MusicManager)
@end
.m
使用方式
@interface ZQMusicManager : NSObject
ZQSingletonH(MusicManager)
@end
VC
創(chuàng)建使用
- (void)viewDidLoad {
[super viewDidLoad];
ZQMusicManager *m = [ZQMusicManager sharedMusicManager];
}
結(jié)尾:
一個(gè)熱愛iOS編程的小伙渣锦。
轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。