Singleton Pattern

Posted by YinChangdao on December 19, 2017

单例

只能共享不能复制的资源

严格实现

创建

+ (instancetype)sharedMediaData {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

+ (id)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)retain {
	return self;
}

- (NSUInteger)retainCount {
	return NSUIntegerMax;
}

- (void)release {
	// 什么也不做
}

- (id)autorelease {
	return self;
}

子类化一个单例类

alloc调用被转发给super, 意味着如果不做修改地子类化单例,返回的实例将总是Singleton。使用一些Foundation中的函数,可以根据类的类型实例化任何对象。

// 在父类的创建方法中可以改为使用 NSAllocateObject
+ (instancetype)sharedMediaData {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [NSAllocaceObject([self class], 0, NULL) init];
    });
    return _instance;
}