笔记1:单例的两种模式

单例有两种模式,一种是饿汉式,一种是懒汉式.所谓懒汉式即使用时创建对象,类似于懒加载。饿汉式即为当程序运行加载到内存时就创建。

> 懒汉式的创建方式

+ (void)initialize {
    if (_shareIntance == nil) {
         NSLog(@"load---我被创建了");
        _shareIntance = [[self alloc] init];
    }
}

程序一运行就会创建


> 饿汉式的创建方式

#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---我被创建了");
        _shareIntance = [[self alloc] init];
    }
}

运行结果


常用gcd方式来实现单例,完整形式如下

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;
}

注意:单例不可继承
项目里有很多单例时,可使用宏创建

// .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创建使用

- (void)viewDidLoad {
    [super viewDidLoad];
    ZQMusicManager *m = [ZQMusicManager sharedMusicManager];
}

结尾:
一个热爱iOS编程的小伙。
转载请附上原文出处链接及本声明。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。