美文网首页
iOS单例设计模式的实现

iOS单例设计模式的实现

作者: 听海听心 | 来源:发表于2017-12-12 11:00 被阅读8次

今天和大家一起来讨论如何进行iOS单例设计的实现,有疏忽的地方,还望各位不吝赐教。


一、单例模式的简介

单例模式的作用:

1、可以保证在程序运行过程中,一个类只有一个实例,而且该实例易于外界访问
2、方便的控制了实例个数,节约系统资源

使用场合:

在整个应用程序中,共享一份资源

二、单例实现

1、ARC模式下 以GHSingleTool为例

// GHSingleTool.h 文件
@interface GHSingleTool : NSObject<NSCopying,NSMutableCopying>
// 类方法
// 1、方便访问
// 2、标明身份
// 3、注意:share+类名|default+类名|share|default
+(instancetype)shareTool;
@end

// GHSingleTool.m 文件
@implementation GHSingleTool
// 1、提供全局变量
static GHSingleTool *_instance;

// 2、之所以不重写alloc方法的原因,是因为执行alloc方法时还是要调用allocWithZone方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    // 1、懒加载写法 但是在多线程中并不适用,解决方案是加锁
    @synchronized (self) {
        if (_instance == nil) {
            // 不知道系统是如何分配内存的就交给父类去做
            _instance = [super allocWithZone:zone];
        }
    }
    
    // 2、使用GCD的方式实现 本身是线程安全的
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

// 3、提供类方法,方便调用
+(instancetype)shareTool{

    return [[self alloc] init] ;
}
// 4、严谨写法 防止用户使用copy或者mutableCopy创建对象
- (id)copyWithZone:(NSZone *)zone{

    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone{

    return _instance;
}

2、MRC模式下 以GHSingleTool为例
关于MRC方式下单例实例化一次,因为在整个程序中都不会被释放,所以不用考虑引用计数的情况,所以只需要将retain方法和release方法手动进行实现即可。

// 如何修改为MRC模式
修改环境为MRC的方式:Build Settings -> Automatic Reference Counting 修改为NO即可。

// GHSingleTool.h 文件
@interface GHSingleTool : NSObject<NSCopying,NSMutableCopying>
// 类方法
// 1、方便访问
// 2、标明身份
// 3、注意:share+类名|default+类名|share|default
+(instancetype)shareTool;
@end

// GHSingleTool.m 文件
@implementation GHSingleTool
// 1、提供全局变量
static GHSingleTool *_instance;

// 2、之所以不重写alloc方法的原因,是因为执行alloc方法时还是要调用allocWithZone方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    // 1、懒加载写法 但是在多线程中并不适用,解决方案是加锁
    @synchronized (self) {
        if (_instance == nil) {
            // 不知道系统是如何分配内存的就交给父类去做
            _instance = [super allocWithZone:zone];
        }
    }
    
    // 2、使用GCD的方式实现 本身是线程安全的
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

// 3、提供类方法,方便调用
+(instancetype)shareTool{

    return [[self alloc] init] ;
}
// 4、严谨写法 防止用户使用copy或者mutableCopy创建对象
- (id)copyWithZone:(NSZone *)zone{

    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone{

    return _instance;
}
// 5、retain方法和release方法手动进行实现(什么都不做)
-(oneway void)release
{
}
-(instancetype)retain
{
    return _instance;
}
// 一般规定返回最大值,告诉别人这里是单例。
-(NSUInteger)retainCount
{
    return MAXFLOAT;
}

三、单例实现分析

1、因为以上两种方式只能兼顾一边,所以可以通过条件编译进行实现。

条件编译写法 只需要把MRC下的方法放到指定的位置即可
#if __has_feature(objc_arc)
// 条件满足 ARC
#else
// 条件满足 MRC
#endif

2、如果你的项目中有很多类需要实现单例,可能就要写很多的重复代码,如何进行代码复用?

  • 首先你可能想到继承,但是单例不能使用继承。因为如果使用继承,父类提供的全局变量会被子类继承,如此造成的问题是,如果父类先实现,子类再进行实现,子类就变成了父类的类型,如果子类先实现,父类再实现,父类就变成了子类的类型,这样会有问题。
  • 最终实现方案,抽取一个宏定义来进行实现 关于宏定义的说明:
    在宏定义中的连接符号是 \ ,宏定义中关于带参数的的声明方式如下
    1、 使用## + 字符串的格式 类似: #define SingleH(name) +(instancetype)share##name;
    2、当你使用的时候()中的name会将##之后的name替换。
// 1、创建一个Single.h文件 
// 2、实现代码如下
 #define SingleH(name) +(instancetype)share##name;
#if __has_feature(objc_arc)
//条件满足 ARC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}

#else
//MRC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
#endif
// 注意:#endif的上一行不能写连接符号 \
  • 使用方式:以创建的Person类为例
// Person.h 文件

#import "single.h"

@interface Person : NSObject

SingleH(person);

@end

// Person.m 文件

#import "Person.h"

@implementation Person

SingleM(person);

@end

写在最后的话:关于iOS单例相关的知识今天就分享到这里,关于iOS单例方面的问题欢迎大家和我交流,共同进步,谢谢各位。

相关文章

  • 设计模式第二篇、单例设计模式

    目录1、什么是单例设计模式2、单例设计模式的简单实现3、单例设计模式面临的两个问题及其完整实现4、单例设计模式的应...

  • iOS模式设计之--创建型:1、单例模式

    iOS模式设计之--1、单例模式

  • 单例模式 Singleton Pattern

    单例模式-菜鸟教程 iOS中的设计模式——单例(Singleton) iOS-单例模式写一次就够了 如何正确地写出...

  • 单例设计模式

    单例设计模式 单例设计模式介绍 单例设计模式的八种方法2.1 饿汉式(静态常量)2.1.1 实现步骤2.1.2 代...

  • 【设计模式】单例模式

    学习文章 iOS设计模式 - 单例 SwiftSingleton 原理图 说明 单例模式人人用过,严格的单例模式很...

  • iOS单例设计模式的实现

    今天和大家一起来讨论如何进行iOS单例设计的实现,有疏忽的地方,还望各位不吝赐教。 一、单例模式的简介 单例模式的...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 设计模式

    手写单例模式(线程安全) 你知道几种设计模式?单例模式是什么?Spring中怎么实现单例模式?

  • 单例模式的常用实现方式

    单例模式属于最常用的设计模式,Java中有很多实现单例模式的方式,各有其优缺点 实现方式对比 单例实现方式线程安全...

  • Java中单例模式你用的哪一种?

    一起讨论java中的单例模式。单例模式是java设计模式中算是最简单的设计模式了。 * java实现单例模式的写法...

网友评论

      本文标题:iOS单例设计模式的实现

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