今天发现原来有两种可实现单例模式的方法,现在贴出来与大家分享
第一种:这个是以前一直在使用的。
static Control *mControl;
...
+(id)getShare
{
if (mControl == nil) {
mControl = [[Control alloc] init];
}
return mControl;
}
第二种:这是今天新发现的。
+ (BDMultiDownloader *)shared
{
static dispatch_once_t once;
static BDMultiDownloader * singleton;
dispatch_once(&once, ^ { singleton = [[BDMultiDownloader alloc] init]; });
return singleton;
}
dispatch_once_t这是系统函数