码迷,mamicode.com
首页 > 其他好文 > 详细

单例实现(单例宏)

时间:2015-01-12 01:32:26      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

一、什么是单例

     单例就是,一个类,每次创建都是同一个对象。也就是说只能实例化一次。

二、如何保证每次创建都是同一个对象

     创建一个对象归根揭底都会经过一个途径,alloc方法(alloc方法会调用allocWithZone:)。因此只要保证alloc方法只会调用一次,且保证线程安全,然后把此对象放在静态区。以后不管是创建对象还是copy对象都直接返回静态区的对象。

三、注意点

     静态全局变量不需要考虑释放的问题(适用于MRC),解决线程安全问题可以用互斥锁或者GCD,后者更好。

     也可设置不让对象重复初始化,即让初始化方法只能执行一次。

四、具体实现代码如下

@implementation myManager
static id instance;

+ (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, ^{
        if (instance == nil)
        {
            instance = [super allocWithZone:zone];
        }
    });
    return instance;
}

+ (instancetype)sharedSoundTools
{
    instance = [[self alloc] init];
    return instance;
}

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

#pragma mark - MRC 部分代码
- (oneway void)release
{
    // 什么都不做
}

- (instancetype)retain
{
    // 本想什么都不做,但它要返回值
    return instance;
}

- (instancetype)autorelease
{
    return instance;
}

- (NSUInteger)retainCount
{
    // 此处防治有人不明就里的粗暴释放对象,比如while循环
    return ULONG_MAX;
}

- (instancetype)init
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        self = [super init];
        if (self)
        {
            self.age = 10; // 只是举个例子,只初始化一次,可以不设置
        }
    });
    return self;
}

@end

 

五、单例宏

      以下代码写在单独singleton.h文件里,使用时直接包含.h头文件。

      使用方法,.h文件:singletonInterface(myManager);

                    .m文件:singletonImplementation(myManager);

      抽取代码如下:

#define singletonInterface(className)          + (instancetype)shared##className;

#if __has_feature(objc_arc)
// 以下是ARC版本
#define singletonImplementation(className) + (instancetype)allocWithZone:(struct _NSZone *)zone {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         if (instance == nil) {             instance = [super allocWithZone:zone];         }     });     return instance; } + (instancetype)shared##className {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         instance = [[self alloc] init];     });     return instance; } - (id)copyWithZone:(NSZone *)zone {     return instance; }
#else
// 以下是MRC版本
#define singletonImplementation(className) + (instancetype)allocWithZone:(struct _NSZone *)zone {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         if (instance == nil) {             instance = [super allocWithZone:zone];         }     });     return instance; } + (instancetype)shared##className {     static dispatch_once_t onceToken;         dispatch_once(&onceToken, ^{         instance = [[self alloc] init];     });     return instance; } - (id)copyWithZone:(NSZone *)zone {     return instance; } - (oneway void)release {} - (instancetype)retain {return instance;} - (instancetype)autorelease {return instance;} - (NSUInteger)retainCount {return ULONG_MAX;}
#endif

// 提示末尾一行不要有 \

 

单例实现(单例宏)

标签:

原文地址:http://www.cnblogs.com/yaann/p/4216035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!