Tuesday, October 14, 2014

Singleton design pattern in java

Singleton patterns comes under creational design patterns. The Singleton's purpose is to control object creation, limiting the number of objects to one only.  Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.

Singleton ensures that a class has only one instance, and provide a global point of access to it.

Singleton applications

1.     Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
2.     Facade objects are often Singletons because only one Facade object is required.
3.     State objects are often Singletons.
4.     The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
5.     The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
6.     When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
7.     Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.

Check list

1.     Define a private static attribute in the "single instance" class.
2.     Define a public static accessor function in the class.
3.     Do "lazy initialization" (creation on first use) in the accessor function.
4.     Define all constructors to be protected or private.
5.     Clients may only use the accessor function to manipulate the Singleton.

Singleton Implementation

public class Singleton {
  // Private constructor prevents instantiation from other classes
  private Singleton() {}

   * SingletonHolder is loaded on the first execution of Singleton.getInstance()
   * or the first access to SingletonHolder.INSTANCE, not before.
  private static class SingletonHolder {
    private static final Singleton INSTANCE = new Singleton();

  public static Singleton getInstance() {
    return SingletonHolder.INSTANCE;