典型的单例的写法:
+ (id) sharedThemeManager {
if (s == nil) {
s = [[[self class] alloc] init];
}
return s;
}
加锁的写法:
+ (id) sharedThemeManager { @synchronized(self){ if (s == nil) { s = [[[self class] alloc] init]; } } return s; }
GCD的写法:+ (id) sharedThemeManager { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if (s == nil) { s = [[[self class] alloc] init]; } }); return s; }
|