书写一个完整的单例

前言

什么是单例?

一个类只允许有一个实例,在整个程序中需要多次使用,共享同一份资源的时候,就可以创建单例,一般封装成工具类使用,苹果封装成单例常用的有 UIApplication,NSUserDefaults,NSNotificationCenter,NSFIleManager等等。

单例的实现

我以前的写法 (不严谨的写法)

// Singleton.h

@interface Singleton : NSObject
+ (instancetype)sharedInstance;
@end

//  Singleton.m
static Singleton *_instance = nil;
@implementation Singleton
+(instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[Singleton alloc] init];
    });
    return _instance;
}
@end

这不算真正意义上的单例,不能保证全局的唯一性,因为类方法和 [ [Class alloc] init ] 方法创建的单例内存地址可能不一样,所有不严谨。

下面举个栗子��

Singleton *singOne = [Singleton sharedInstance];
Singleton *singTwo = [Singleton sharedInstance];
Singleton *singThere = [Singleton new];
Singleton *singFour = [[Singleton alloc] init];
NSLog(@"singOne:-> %@",singOne);
NSLog(@"singTwo:-> %@",singTwo);
NSLog(@"singThere:-> %@",singThere);
NSLog(@"singFour:-> %@",singFour);

这时我看查看一下控制台输出的信息

2018-02-07 10:46:25.024988+0800 Markdown[2056:67507] singOne:-> <Singleton: 0x604000001a00>
2018-02-07 10:46:25.025222+0800 Markdown[2056:67507] singTwo:-> <Singleton: 0x604000001a00>
2018-02-07 10:46:25.025372+0800 Markdown[2056:67507] singThere:-> <Singleton: 0x6040000019a0>
2018-02-07 10:46:25.025646+0800 Markdown[2056:67507] singFour:-> <Singleton: 0x604000001a10>

结果很明显, [Singleton sharedInstance] 类方法创建出来单例singOnesingTwo 的内纯地址是一样的,说明单例创建对了,但是看到newalloc init 创建的singTheresingFour 的内存地址不一样,同时也与singOnesingTwo的地址不一样.

下面怎么聊聊怎么解决这个问题

百度一下还真有这类的帖子和博客,我们主要的问题就是保证单例的唯一性,避免不小心用实例方法创建单例,所有应该保证alloc initnewcopy 方法创建的单例的的唯一性。

在创建对象的时候主要分这么两步 alloc (申请内存)init(初始化)

  1. 我们在第一步alloc的会后就要对其进行拦截。当我们去调用alloc的时候,OC内部会调用allocWithZone这个方法去申请内存,我们去覆写这个方法,然后在这个方法中调用之前的类方法,返单例对象,这样就能达到我们的目的了。
  2. 拷贝对象也是一样的,覆写copyWithZone方法,然后在方法中去调用类方法,返回单例对象。(在覆写copyWithZone方法之前别忘记了签署NSCopying协议)

  3. 参考:书写一个严谨的单例

下面修改一下 Singleton.m
参考:iOS-单例模式简单使用

// Singleton.m

static Singleton *_instance = nil;
@implementation Singleton
+(instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[super alloc]init];
    }
    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone{
    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone{
    return _instance;
}

看一下控制台输出的结果:

2018-02-07 11:20:46.993693+0800 Markdown[2374:88765] singOne:-> <Singleton: 0x604000001990>
2018-02-07 11:20:46.993906+0800 Markdown[2374:88765] singTwo:-> <Singleton: 0x604000001990>
2018-02-07 11:20:46.994040+0800 Markdown[2374:88765] singThere:-> <Singleton: 0x604000001990>
2018-02-07 11:20:46.994146+0800 Markdown[2374:88765] singFour:-> <Singleton: 0x604000001990>

很明显,改过 Singleton.mSingleton类方法的类方法sharedInstance 和 实例方法 alloc init 的方法创建的单例内存地址一样了,是不是有点小喜悦。

你以为这就完事了吗? NO、NO、NO

哪个单例里面没有几个属性,最少也得有一个吧,要不然我要这铁(dan)棒(li)有何用。
这就加几个属性试试。

Singleton *singOne = [Singleton sharedInstance];
SingOne.array = @[@"1",@"2",@"3"];
Singleton *singTwo = [Singleton sharedInstance];
SingOne.array = @[@"4",@"5",@"6"];
Singleton *singThere = [Singleton new];
SingThere.array = @[@"7",@"8",@"9"];
Singleton *singFour = [[Singleton alloc] init];
SingFour.array = @[@"0",@"0",@"0"];
NSLog(@"  singOne:-> %@ , %p , %@ ",singOne,singOne.array,singOne.array);
NSLog(@"  singTwo:-> %@ , %p , %@ ",singTwo,singTwo.array,singTwo.array);
NSLog(@"singThere:-> %@ , %p , %@ ",singThere,singThere.array,singThere.array);
NSLog(@" singFour:-> %@ , %p , %@ ",singFour,singFour.array,singFour.array);

现在看看结果
array 属性地址唯一,数组内容也唯一,满足标准。

2018-02-07 11:48:39.843225+0800 Markdown[2804:110175]   singOne:-> <Singleton: 0x60000003ec60> , 0x600000445250 , (
    0,
    0,
    0
)
2018-02-07 11:48:39.843439+0800 Markdown[2804:110175]   singTwo:-> <Singleton: 0x60000003ec60> , 0x600000445250 , (
    0,
    0,
    0
)
2018-02-07 11:48:39.843589+0800 Markdown[2804:110175] singThere:-> <Singleton: 0x60000003ec60> , 0x600000445250 , (
    0,
    0,
    0
)
2018-02-07 11:48:39.843713+0800 Markdown[2804:110175]  singFour:-> <Singleton: 0x60000003ec60> , 0x600000445250 , (
    0,
    0,
    0
)

声明

感谢以上两位博主的文章,借鉴做了一份单例的笔记,记录开发中的问题并解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值