mercredi 8 novembre 2017

Singleton design patters and Differences between different types?

There are different types of singleton implementation.

First:

static MyGlobalClass *instance = nil;
+(MyGlobalClass*)myinstance
{
@synchronized(self)
{
    if(instance==nil)
    {
        instance= [MyGlobalClass new];
   }
}
return instance;
}

Second:

+(PKShareClass *)sharedInstance
{
static PKShareClass *shaedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    shaedInstance = [[PKShareClass alloc]init];
});

return shaedInstance;}

And finally with

static NSOperationQueue * _connectionQueue = nil;
+ (NSOperationQueue *) connectionQueue{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    if (!_connectionQueue)
    {
        _connectionQueue = [[NSOperationQueue alloc] init];

    }
});
return _connectionQueue;
}

Here my question is what it means when we initialize like first and second ?? And third one with NSOperationQueue.What is the use when we initialize like third one? I have been searching a lot but hard to find the meaning. If possible provide some links I can explore ?? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire