iOS单例设计模式的实现

2017-12-12  本文已影响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、创建一个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.h 文件

#import "single.h"

@interface Person : NSObject

SingleH(person);

@end

// Person.m 文件

#import "Person.h"

@implementation Person

SingleM(person);

@end

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

上一篇 下一篇

猜你喜欢

热点阅读