iOS单例设计模式的实现

今天和大家一起来讨论如何进行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单例方面的问题欢迎大家和我交流,共同进步,谢谢各位。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,506评论 19 139
  • 单例模式(SingletonPattern)一般被认为是最简单、最易理解的设计模式,也因为它的简洁易懂,是项目中最...
    成热了阅读 4,530评论 4 34
  • 在iOS中有很多的设计模式,有一本书《Elements of Reusable Object-Oriented S...
    郑明明阅读 2,620评论 3 26
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,624评论 18 399
  • “冥冥中注定”或许只会是电影或者文学里面的桥段,但如此“天命”却着着实实的发生在黄雨生身上了,他曾经在本科大二时发...
    未醒語笙阅读 728评论 0 0

友情链接更多精彩内容