iOS 单例模式

来源:互联网 发布:网络监控头更改ip 编辑:程序博客网 时间:2024/06/09 20:40
    最近在iOS开发中,需要用到单例模式,于是自己动手写了一个,它看起来是这样的:

<1>

+ (id)sharedInstance{    static id sharedInstance = nil;        if (!sharedInstance) {        sharedInstance = [[NSObject alloc] init];    }        return sharedInstance;}

    后来发现许多书上的做法都使用到了BOOL变量作为标值位,它看起来是这样的:

<2>

+ (id)sharedInstance{    static id sharedInstance = nil;    static BOOL token = NO;        if (!token) {        token = YES;                sharedInstance = [[NSObject alloc] init];    }        return sharedInstance;}

    但是参考了苹果官方的单例模式代码,发现它看起来是这样的:

<3>

+ (id)sharedInstance{    static id sharedInstance;    static dispatch_once_t onceToken;        dispatch_once(&onceToken, ^{        sharedInstance = [[NSObject alloc] init];    });        return sharedInstance;}

    另外一种线程安全的方法:

<4>

+ (QLog *)log    // See comment in header.{    static QLog * sLog;        // Note that, because we can be called by any thread, we run this code synchronised.     // However, to avoid synchronised each time, we do a preflight check of sLog.     // This is safe because sLog can never transition from not-nil to nil.        // - by waterforest    // For amazing singleton. So, this method is also thread safe.    // There are two amazing singleton ways: 1. GCD 2. @synchronized.        if (sLog == nil) {        @synchronized ([QLog class]) {            if (sLog == nil) {                sLog = [[QLog alloc] init];                assert(sLog != nil);            }        }    }    return sLog;}

    那么它们究竟有多大区别呢?

    原来,它们的区别在于多线程并发时的表现。

    <1>使用了一个指针变量作为标志位,这在多线程并发时是不可取的,因为sharedInstance = [[NSObject alloc] init];这行代码的执行本身是需要时间的。很可能有两个线程同时进入到了这行代码,而这将导致内存泄漏。

    <2>使用的标志位是一个BOOL变量,当多线程并发时,会出现1线程判断为NO,开始alloc并做赋值操作,但是2线程进入时判断为YES,而1线程赋值操作还没执行结束,这时候2线程会拿到一个nil。尽管它不会造成内存泄漏,但是它会有相当多的线程获取不到对象。

    <3>使用了dispatch_once函数。这个函数来自于Grand Central Dispatch (GCD),Apple自Mac OS 10.6 / iOS 4.0引用了它。

    该函数接收一个dispatch_once_t用于检查该代码块是否已经被调度的谓词(是一个长整型,实际上作为BOOL使用)。它还接收一个希望在应用的生命周期内仅被调度一次的代码块。这不仅意味着代码仅会被运行一次,而且还是线程安全的,你不需要使用诸如@synchronized之类的来防止使用多个线程或者队列时不同步的问题。

    Apple的GCD Documentation证实了这一点:

    如果被多个线程调用,该函数会同步等等直至代码块完成。