美文网首页
单例模式 实现(MRC和ARC)和宏抽取

单例模式 实现(MRC和ARC)和宏抽取

作者: GrayMantis | 来源:发表于2017-10-10 22:55 被阅读0次

MRC环境-单例实现

.h文件实现

@interface DemoSoundTool : NSObject
+ (instancetype)sharedSoundTool;

.m文件实现

实现一个类方法
重写五个系统方法

#import "DemoSoundTool.h"

@implementation DemoSoundTool

static DemoSoundTool *_soundTool = nil;

/**
 *  alloc方法内部会调用allocWithZone:
 *  zone : 系统分配给app的内存
 */
+ (id)allocWithZone:(struct _NSZone *)zone
{
    if (_soundTool == nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{ // 安全(这个代码只会被调用一次)
            _soundTool = [super allocWithZone:zone];
        });
    }
    return _soundTool;
}

- (id)init
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _soundTool = [super init];
    });
    return _soundTool;
}

+ (instancetype)sharedSoundTool
{
    return [[self alloc] init];
}

- (oneway void)release
{

}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return 1;
}

ARC环境下- 单例实现

不重写 release, retain, retainCount这三个函数即可, 其他 和 MRC一样

当需要定义多个 单例的时候, 每个单例内部实现基本一样, 这时可以[ 抽取一个宏 ] 实现单例, 减少代码量开支

定义一个头文件singleton.h

// 帮助实现单例设计模式

// .h文件的实现
#define SingletonH(methodName) + (instancetype)shared##methodName;

// .m文件的实现
#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

宏定义之后,就可以一句话搞定 单例

例如在MRC环境下 , 单例模式可以这样实现

.h文件实现

#import "Singleton.h"
@interface DemoSoundTool : NSObject

SingletonH(SoundTool)

.m文件实现

#import "Singleton.h"
#import "DemoSoundTool.h"

@implementation DemoSoundTool

//这样一句话就搞定了
SingletonM(SoundTool)

@end

相关文章

  • 单例模式 实现(MRC和ARC)和宏抽取

    MRC环境-单例实现 .h文件实现 .m文件实现 实现一个类方法重写五个系统方法 ARC环境下- 单例实现 当需要...

  • iOS浅谈单例

    ARC MRC 抽取单例 单例模式是不能使用继承的 具体使用

  • 单例模式(单例宏)

    单例模式 单例模式(arc) 类的实现 调用单例 单例模式(mrc) 除了上边的方法我们在mrc的时候还需要增加一...

  • iOS单例一行实现(抽取单例宏)

    本文首先实现单例模式,然后对单例代码进行抽取宏,使其他类可以一句代码实现单例(只介绍ARC环境)本文代码 - 单例...

  • iOS 单例模式

    概念相关 (1)单例模式 (2)使用场合 2 ARC实现单例 (1)步骤 (2)相关代码 3 MRC实现单例 (1...

  • The Singleton Pattern 单例模式

    单例模式的作用 单例模式的使用场合 单例模式在ARC\MRC环境下的写法有所不同,需要编写2套不同的代码 可以用宏...

  • 单例模式

    这篇文章的主要内容包括:1、在ARC中完成一个单例模式的三步2、在MRC中完成一个单例模式的三步3、单例模式通用宏...

  • 迅雷

    手撕一个线程同步安全的单例模式 内存管理机制 MRC和ARC的区别以及ARC是如何不需要手动加一减一的 MRC和A...

  • 单例模式之ARC和MRC环境下实现单例模式

    单例模式的作用可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问从而方便地控制了实例个数,并节约...

  • OC语言day08-21单例ARC和MRC写法 (Single

    pragma mark 单例ARC和MRC写法 (Singleton) pragma mark 概念 pragm...

网友评论

      本文标题:单例模式 实现(MRC和ARC)和宏抽取

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