美文网首页
完整单例模式写法

完整单例模式写法

作者: 季末微夏 | 来源:发表于2017-04-14 14:33 被阅读163次

前言

关于单例模式,在开发中我们经常使用到,在此作一个记录。既然是单例,那么我们就应该保证通过各种方式初始化创建的对象是同一个。

代码

//第1步: 存储唯一实例
static StatisticsHelper *_helper = nil;
//第2步: 分配内存空间时都会调用这个方法. 保证分配内存alloc时都相同.
+(id)allocWithZone:(struct _NSZone *)zone{
    //调用dispatch_once保证在多线程中也只被实例化一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _helper = [super allocWithZone:zone];
    });
    return _helper;
}
//第3步: 保证init初始化时都相同
+(instancetype)sharedTools{
    return [[self alloc] init];
}
-(id)init{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _helper = [super init];
    });
    return _helper;
}
//第4步: 保证copy时都相同
-(id)copyWithZone:(NSZone *)zone{
    return _helper;
}
//第五步: 保证mutableCopy时相同
- (id)mutableCopyWithZone:(NSZone *)zone{
    return _helper;
}

单例宏

经常使用单例,难免需要写很多代码,这里贴出单例宏方便大家使用。

//-------------------单例宏
// .h文件的实现
#define SingletonH(className) + (instancetype)shared##className;

#if __has_feature(objc_arc) // 是ARC
// .m文件的实现
#define SingletonM(className) \
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##className \
{ \
return [[self alloc] init]; \
} \
- (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
- (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}

#else // 不是ARC

#define SingletonM(className) \
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##className \
{ \
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
//----------单例宏

总结

其实东西并不多,但是需要你去了解各种初始化方法,知道他们的区别。

相关文章

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • 完整单例模式写法

    前言 关于单例模式,在开发中我们经常使用到,在此作一个记录。既然是单例,那么我们就应该保证通过各种方式初始化创建的...

  • 第03条 用私有构造方法或者枚举类型强化Singleton属性

    单例模式最佳写法1 - 双重校验锁 单例模式最佳写法2 - 静态内部类

  • 单例模式

    单例模式的写法

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • Kotlin中的单例模式与Java对比

    目前java中的单例模式有多种写法,kotlin中的写法更多一点,本篇会总结全部的到单例模式写法。 一、懒人写法(...

  • 单例模式

    一、介绍 二、单例模式代码实现 三、单例的简介写法

  • java 24 设计模式

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

  • Android中常见的内存泄漏汇总

    1.单例模式的错误写法 单例模式的正确写法: 2.非静态内部类创建静态实例造成的内存泄漏错误写法 正确写法:将该内...

  • 单例模式

    单例模式--概念 单例模式常见的写法有:懒汉式,饿汉式,登记式。单例模式的特点有:1.单例类只能有1个实例2.单例...

网友评论

      本文标题:完整单例模式写法

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