Showing posts with label singleton. Show all posts
Showing posts with label singleton. Show all posts

Thursday, November 8, 2012

dispatch_once

dispatch_once is synchronous and its saying "perform something once and only once".It replaces the following:

+ (MyClass *)sharedInstance {
    static MyClass *sharedInstance;
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[MyClass alloc] init];
        }
    }
    return sharedInstance;
}
Advantage of dispatch_once is its faster.

Monday, September 10, 2012

Singleton Design Pattern

Singleton Pattern is a design pattern that restricts the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
Refer the link: http://www.galloway.me.uk/tutorials/singleton-classes/