不使用GCD
#import "ServiceManager.h"
static ServiceManager * defaultManager;
@implementation ServiceManager
+ (ServiceManager * )defaultManager {
if ( ! defaultManager)
defaultManager = [[self allocWithZone : NULL] init];
return defaultManager;
}
@end
使用GCD
#import "ServiceManager.h"
@implementation ServiceManager
+ (ServiceManager * )sharedManager {
static dispatch_once_t predicate;
static ServiceManager * sharedManager;
dispatch_one(&predicate,^{
sharedManager = [[ServiceManager alloc] init];
});
return sharedManager;
}
@end