单例模式的两种实现,

1、@synchronized

static InstanceClass *instance;
+ (InstanceClass *)defaultInstance{
    @synchronized (self){
        if (instance == nil) {
            instance = [[InstanceClass alloc] init];
        }
    }
    
    return instance;
}

2、GCD

static InstanceClass *instance;
+ (InstanceClass *)defaultInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[InstanceClass alloc] init];
    });
    
    return instance;
}

 

posted @ 2013-08-24 08:30  路在脚下,  阅读(161)  评论(0编辑  收藏  举报