Wednesday, August 4, 2010

Singleton Design Pattern

It is a special class, for which we can not have more than one instance in system at a time. It provides a single global point of access to that instance. Hence it ensures that only one instance can be created for this class and every user is forced to use only that single instance for any operation.


Example

  • Any Utility Class: Generally utility classes do not have states with these and so a single instance of that class can serve the whole system.
  • Memory Manager: An object which is responsible to manage the memory in system. Obviously only there should be only one instance of this object so that it can update the objects with the current memory availability and other objects can update its state for expected memory requirement. 

When to Use

  • When only one instance can solve the purpose like utility classes, and does not contain any state with it.
  • When only one instance is must to preserve and synchronize the entire system states,  so to share the state. 


Usage

  • Helps to ensure only one object in memory.
  •  Helps to share the states across the system. 


Drawbacks

  • Difficult to extend any singleton class as it will work only if super class has not been instantiated by now.

7 comments:

  1. Singleton class should override clone method also, so that no one can make a clone of it.

    ReplyDelete
  2. Ensure following things if create singleton class:
    (1)If singleton class working in multi threaded environment then its getInstance method must be synchronized.
    public static synchronized getInstance()
    {
    // return class object
    }
    (2)Override clone method in your class and force to not create clone object.

    And one more thing Utility class can be static but it is suggested Utility class should be singleton.

    ReplyDelete
  3. Hi Sir thanks for this nice article.

    We use Singleton pattern in our projects while accessing database. We use below three classes:

    Abstract DataProvider class: This has a static method that returns instance of DataProvider class.

    SqlDataProvider class: This public class extends abstract data provider class

    Controller class at top calling methods provided by above classes, like below:
    DataProvider.Instance().GetValue(param1, param2);

    My question is that will I someday face race around condition and if yes, how to avoid it?

    ReplyDelete
  4. Hello Ravi, There should not be any race condition here. DataProvider is an abstract class which can not be instantiated. So it is only SQLDataProvider or any other extended provider class, which will be instantiated.

    Singleton pattern can be void if super class can also be instantiated separately and hence you can not control the one instance of super.

    Hope it answer the question.

    ReplyDelete
  5. Here is a good article defining various ways to implement Singleton - http://www.informit.com/articles/article.aspx?p=1216151&seqNum=3

    ReplyDelete
  6. If you make your implemented class a final class, it will stop breaking the rule of singleton.

    ReplyDelete