美文网首页
笔记1:单例的两种模式

笔记1:单例的两种模式

作者: 热血足球2016 | 来源:发表于2019-09-29 11:04 被阅读0次

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

> 懒汉式的创建方式

+ (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编程的小伙。
转载请附上原文出处链接及本声明。

相关文章

  • Swift的单例模式及如何处理并发访问

    Swift的单例模式 Swift有两种方式实现单例模式 1、全局常量 2、类型常量 处理单例模式的并发访问 1、多...

  • 2019-08-27 java设计模式之单例模式

    1.单例模式概述 java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍两种:懒汉式单例、饿...

  • 单例模式

    单例模式(饿汉式) 单例模式(懒汉式) 解决两种的弊端的单例模式(双锁)

  • 单例模式之枚举类enum

    通过枚举实现单例模式 枚举类实现单例模式的优点 对于饿汉式单例模式和懒汉式单例模式了解的同学,使用以上两种单例模式...

  • Python经典面试题21道

    1、Python如何实现单例模式? Python有两种方式可以实现单例模式,下面两个例子使用了不同的方式实现单例模...

  • Python最经典面试题21道,必看!

    1、Python如何实现单例模式? Python有两种方式可以实现单例模式,下面两个例子使用了不同的方式实现单例模...

  • Python 经典面试题

    1、Python如何实现单例模式? Python有两种方式可以实现单例模式,下面两个例子使用了不同的方式实现单例模...

  • Python 经典面试题

    1、Python如何实现单例模式? Python有两种方式可以实现单例模式,下面两个例子使用了不同的方式实现单例模...

  • 浅谈单例模式

    如有错误还望指正!!! Java单例模式说明: 在Java中单例模式分为两种:1.饿汉式单例:在定义实例以供外部使...

  • Java单例模式以及与静态方法的一些区别

    Java单例模式说明: 在Java中单例模式分为两种:1.饿汉式单例:在定义实例以供外部使用的时候直接实例化对象,...

网友评论

      本文标题:笔记1:单例的两种模式

      本文链接:https://www.haomeiwen.com/subject/onxhyctx.html