我在任何地方都看到了单例模式的这种特殊实现:
+ (CargoBay *)sharedManager {
static CargoBay *_sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[CargoBay alloc] init];
});
return _sharedManager;
}而且它似乎被认为是良好的实践(尤其是来自CargoBay)。
我唯一不明白的部分是第一行static CargoBay *_sharedManager = nil;。
为什么要将static变量设置为nil
发布于 2012-09-22 15:03:13
这只是一个可读性,惯例和实践的问题。并不是真的需要,因为:
一。它的价值永远不会被检查。在旧的单例实现中,曾经有著名的
+ (id)sharedInstance
{
static SomeClass *shared = nil;
if (shared == nil)
shared = [[SomeClass alloc] init];
return shared;
}代码-要使该方法工作,必须将后备变量初始化为零,因为如果它第一次不是零,它将错误地忽略if部分中的alloc-init并返回一个垃圾指针。但是,对于GCD解决方案,不再需要零检查- GCD处理‘只执行此代码一次’的实用化。
二。但是,尽管如此:静态变量被隐式初始化为零。因此,即使您只编写static id shared;,最初也将是nil。
三。为什么这可能是个好做法?因为,尽管我提到了前两个原因,但是让源代码的读者知道某些东西被显式初始化为零仍然是更容易理解的。或者甚至可能存在一些不符合要求的实现,其中静态变量没有被正确地自动初始化,然后将采取此操作。
发布于 2012-09-22 15:08:20
您正在将其设置为零,以确保您正在获得一个干净的实例。
这是您想要做的更易读的版本:
+ (GlobalVariables *)sharedInstance {
// the instance of this class is stored here
static GlobalVariables *myInstance = nil;
// check to see if an instance already exists
if (nil == myInstance) {
myInstance = [[[self class] alloc] init];
}
// return the instance of this class
return myInstance;
}但是有大量的帖子展示了这是如何不可能是线程安全的,所以转移到上面的方法和我发布的内容的混合体上,您可以得到以下信息:
// Declared outside Singleton Manager
static SingletonClass *myInstance = nil;
+ (GlobalVariables *)sharedInstance {
if (nil != myInstance) {
return myInstance;
}
static dispatch_once_t pred; // Lock
dispatch_once(&pred, ^{ // This code is called at most once per app
myInstance = [[GlobalVariables alloc] init];
});
return myInstance;
}https://stackoverflow.com/questions/12544808
复制相似问题