Monday 10 June 2013

Singleton Design Pattern for Objective-C

 

Singleton Design Pattern for Objective-C

Well first of all what is a singleton design pattern -
A singleton class in an object-oriented application always returns the same instance of itself. It provides a global access point for the resources provided by the object of the class. A design pattern that is related to these kinds of designs is called the Singleton pattern.
When Would You Use the Singleton Pattern?
  • There must be exactly one instance of a class with which it must be accessible from a well-known access point.
  • The sole instance can be extended only by subclassing, and it won’t break client code with the extended object.
The Singleton pattern provides a well-known access point to client classes that want to create a unique instance of and access to a shared resource. Although a static global object reference or a class method can provide a global access point, the global object cannot prevent the class getting instantiated more than once, and the class method lacks the flexibility of decoupling.
A static global variable holds a single reference to an instance of a class. Another class or a method that can access that global variable is, in fact, sharing the same copy with other classes or methods that use the same variable. That sounds like what we are after in this chapter. Everything seems fine if we use only the same global variable throughout the whole application. So, in fact, we don’t need the Singleton pattern. But hey, wait a minute; what if there is somebody in your team or a consultant who has defined the same type of static global variable as yours? Then there will be two copies of the same global object type living in the same application—so a global variable doesn’t really solve the problem.
A class method provides shared services without creating an object of it. A single instance of the resource is maintained within the class method. However, this approach lacks the flexibility if the class needs to be subclassed to provide better services.
A singleton class can guarantee a single, consistent, and well-known access point to create and access a single object of the class. The pattern provides the flexibility such that any of its subclasses can override the instance method and have total control over object creation of itself without changing code in the client. Or even better, the instance implementation in the parent class can handle dynamic object creation. The actual type of a class can be determined to make sure the correct object is created at runtime.

Implementing a Singleton in Objective-C -

     MySingleton.h
   
     #import <Foundation/Foundation.h>
   
     @interface MySingleton : NSObject {
   
     }
   
     + (MySingleton*) sharedInstance;
   
     @end

    MySingleton.m
            
     #import "MySingleton.h"
    
     static MySingleton *_instance;
    
     @implementation MySingleton
    
     #pragma mark -
    
     #pragma mark Singleton Methods
    
     + (MySingleton*)sharedInstance
    
     {
    
     @synchronized(self) {
    
     if (_instance == nil) {
    
     _instance = [[self alloc] init];
    
     // Allocate/initialize any member variables of the singleton class here
    
     // example
    
     //_instance.member = @"";
           }
     }
     return _instance;
 }

Visit Apple doc's this page for more info on Singleton Design Pattern -
 
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObject
 

* Working with Core Data:

  http://www.appcoda.com/introduction-to-core-data/
 

No comments:

Post a Comment