Tag Archives: enums

Understand Singletons at one go!

If you are looking for a switch and have 0-2 years experience in Java, then this is one topic that you must know. In this post, I try to explain all aspects of the topic and in the end include all possible questions asked.

Singleton Design Pattern is the most simple but most controversial pattern in terms of complexity of usage. It was basically introduced to solve the problem of allowing only one object to control some resource, and allowing only one copy of the object to exist i.e it provides global point of access to the object. Commonly used in logging, caches, thread pools, configuration settings, device driver objects, etc.

Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

STRUCTURE

Singleton Class Diagram

  • Static member : This contains the instance of the singleton class.
  • Private constructor : This will prevent anybody else to instantiate the Singleton class.
  • Static public method : This provides the global point of access to the Singleton object and returns the instance to the client calling class.

USAGE

Singletons must be used when only a single instance of object is required in memory for a single point of access. For example

  • Accessing application specific properties through a singleton object, which reads them for the first time from a properties file and subsequent accesses are returned from in-memory objects.
  • Creating a session factory. We need one session factory per database and use the same throughout our application. Singleton objects can be used for the same.

EXAMPLES in JDK

  • java.lang.Runtime class (getRuntime() method is used to return instance)
  • java.awt.Desktop class (getDesktop() method is used to return instance)

IMPLEMENTATION (advantages and disadvantages)

Singleton class can be implemented in following ways:

1. Early Initialization (static factory method): In this implementation, singleton gets instantiated at the time of class loading. The object is referenced by a static final field which gets returned each time singleton’s getInstance() method is called.

public class SingletonClass{
           private static final instance=new SingletonClass();
           public static SingletonClass getInstance(){
                                      return instance;
           }
 }

Advantages:

  • Instance cannot be redefined due to usage of final. (ensuring one and only one instance exists)
  • No use of expensive locking or synchronization as all threads see same instance. 

Disadvantages:

  • Since Singletons can be loaded only time per JVM in a class loader, the above implementation fails when the class gets loaded by multiple class loaders.

2. Lazy Initialization : In this implementation, the object of class gets constructed only when class is called.

public class SingletonClass{
          private static instance;
          public static SingletonClass getInstance(){
                    if(instance==null)
                           instance=new SingletonClass();
                                           return instance;
                 }
  }

Advantages:

  • Instance not constructed until class is used.

Disadvantages:

  • Since Singletons can be loaded only time per JVM in a class loader, the above implementation fails when the class gets loaded by multiple class loaders.
  • Unsynchronized i.e if two threads access the same method at same time, they both can create two different objects of the class.

3. Double checked locking : In this implementation, when two threads enter the getInstance() method at the same time, only one thread enters the synchronization block and constructs the instance. The other thread when allowed to enter the critical sections gets the instance and returns the same instance that the previous thread created, hence allowing only one instance per application.

public class SingletonClass{
     private static instance;
           public static SingletonClass getInstance(){
                   if(instance==null){
                synchronized(instance){
                      if(instance==null) 
                           instance=new SingletonClass();
                     }
                           }
         return instance;
          }
}

Advantages:

  • Instance not constructed until class is used.
  • Synchronized upto an extent

Disadvantages:

  • Since Singletons can be loaded only time per JVM in a class loader, the above implementation fails when the class gets loaded by multiple class loaders.
  • Static variables are initialized by JVM to default values if not explicitly initialized. Hence, threads can return the default values, when some thread is inside the critical section and busy constructing the singleton object.

4. Using volatile variable: Volatile variables are thread-safe, and changes by one thread are visible to others.Hence, in this implementation, the instance if changed, will be visible to all threads.

public class SingletonClass
{
      private static volatile SingletonClass instance;
      public static SingletonClass getInstance()
      {
            if( instance == null) 
                        instance = new SingletonClass();
            return instance;
      }
 }

Advantages:

  • OOP properities like inheritance can easily be applied.
  • Thread-safe

Disadvantages:

  • Since Singletons can be loaded only time per JVM in a class loader, the above implementation fails when the class gets loaded by multiple class loaders.

5. Using ENUMS: Singleton implementation using enums were introduced in JAVA 5. Enums are thread-safe and serializable. The properties are guaranteed by JVM itself. Hence, is the most efficient and easiest way to construct Singleton. One can easily access it by MySingleton.INSTANCE.

public enum MySingleton {
  INSTANCE;   
}

Since Singleton Pattern is about having a private constructor and calling some method to control the instantiations (like some getInstance), in Enums we already have an implicit private constructor. And the enum declaration actually compiles to something like

public final class MySingleton {
          public final static MySingleton INSTANCE = new MySingleton(); 
}

When the code first accesses INSTANCE, the class MySingleton will be loaded and initialized by the JVM. This process initializes the static field above once (lazily).

Advantages:

  • Reflection: It has the ability to access private members of any class. Hence, to avoid relection accessing our private constructor, we could either throw an Exception inside the constructor or use ENUM and let JVM handle it itself.
  • Serialiazation: readObject() of serialization always return a new instance of the class to be serialized. Although, we can override the readResolve() method of Serialization and return the same instance, still letting ENUM handle it becomes a better option.
  • Cloning: Multiple instances can be created through cloning. To prevent the another instance to be created of the singleton instance we can throw exception from inside the clone() method. Though enums cannot be cloned.

Disadvantage:

  • Cannot be extended as enums they final.

WHY NOT JUST MAKE THE OBJECT STATIC INSTEAD OF MAKING THE CLASS SINGLETON?

If we go through semantic point of view, to make user understand that only one instance per application exists, making the class Singleton would be the best option. Also, no new object will be created referring to same instance, that later needs to be garbage collected i.e if a class whose object is made static is heavy, then every time new MyClass() gets called, an object gets created occupying memory heap space unnecessary.

SINGLETONS v/s STATIC CLASS

This is a question asked when interviewer wants to know your deep knowledge about singletons. The question asked by him, would be like when static classes can also provide one instance and can be used without creating an object, why do we need Singletons?

When to use Singletons over Static classes:

  • Capable of providing different implementations, but guarantees only one instance per JVM. e.g getRuntime() method of java.lang.Runtime class returns different implementation on different JVM but guarantees one instance per JVM.
  • can be extended and implement interfaces.
  • Can be cloned, serialized, extended and lazily loaded.
  • Easier to test

 When to use Static classes over Singleton:

Classes that provides only static methods are called Static classes. e.g java.lang.Math

  • Faster than Singletons, as methods are bonded at compile time.
  • Due to inadequate synchronization, can lead to subtle race conditions when need to maintain state.
  • Always eagerly loaded.

SINGLETONS: ANTI PATTERN

Singletons are often badly implemented and hard to unit test. The memory allocated to them can’t be freed and excessive usage can lead us to Procedural programming (as with private constructor, there is no need to extend them!). Also, scope for which the instance is unique is hard to find. Because of these disadvantages of Singletons, they are considered to be anti-pattern.

Separate JREs are expected to use separate singleton instances as they can have multiple class-loaders. Global state increases coupling in our code and makes it hard to re-factor. Also, singleton implementation cannot be changed without changing it for all the classes accessing it.

Hence, singletons are avoided by many developers and instead Dependency Injection is recommended. (to be discussed in my next post)

A class which is used (almost) universally, but does not contain state relevant to your application, can safely be implemented as Singleton.

Hope you guys liked my post. Please feel free to ask any questions related to the topic in the comments below. 🙂

Advertisements