Singleton Pattern

As a programmer you should have come across many design patterns, in this blog entry I would like to spend some time on Singleton Design pattern. Before, further discussion on Singleton pattern, let us try understanding what is Design Pattern, and why do we need to learn about these patterns.

A Design pattern, is a specification defined to solve a particular problem. In our day to day programming we will be trying to solve several problems, design patterns are those solutions which are adopted by many developers around the globe to solve those specific problems you face today. Now its time for you to adopt some of those in your code, instead of fighting out your own solution for the problems you have on your table.

Below are few Design patterns, which were mostly adopted by many developers across the globe, in their day to day development.

  • MVC
  • Singleton
  • Factory Object
  • Builder
  • DAO pattern

Let us try to see the problem statements, that can be solved using the Singleton pattern and then we can dig more deeper on to what is a Singleton and what ways we can define singletons.

Problem Statement 1: My JVM goes OOM, because an object with heavy foot print is created several times, by several threads.

Problem Statement 2: I want a shared object across several threads during manipulation, without passing the references of that object.

Problem Statement 3: I want to define a utility class, but don’t want to create all the methods of that utility class as static.

If you, are stuck with any of the problem statements given above, then its time for you to implement a Singleton pattern for those objects. Now, that we know when to use a Singleton pattern, lets see what are its qualities, and what it offers for the developers.

  • Singleton objects can be instantiated only once
  • At any point of time there will be one and only one object in the JVM
  • All the threads will share the same object.

Now that, you have seen what all situations we can use Singleton, and what are the properties of a Singleton, let me show you how can we implement a Singleton pattern. A Singleton can be implemented using several techniques, I will try to cover some of those in this post.

1. Eager Instantiation

2. Lazy Instantiation

a. Locking

b. Double Checked Locking

c. Double Checked locking using volatile keyword

c. Using Inner Class

3. Using Enums as Singletons

Each of the above techniques with implementation details and examples, are explained hereunder, for giving you basic view of each of the technique.

Eager Instantiation:

Eager instantiation means, the object is initialized when the class is loaded, even before there is any actual invocation for getting a reference to that object. This method is not advisable, as you will be creating an object, even before it is needed for your application. If you object loaded this way, is a heavyweight object then you will be wasting a lot of Heap memory.

   1: public class Foo{

   2:     public static Foo foo = new Foo();

   3:     private Foo(){}

   4:     public static Foo getInstance(){

   5:         return foo;

   6:     }

   7: }

Lazy Instantiation:

Lazy instantiation means, the object is initialized when the application requires it, instead of creating at the time the Class is loaded on to the JVM. Lazy instantiation can be done in variety of ways, each having subtle problems which I will be explaining shortly.

Locking: In this method we will make the get Instance method synchronized, this will acquire a lock on the Class type, and any threads that want to access the methods of this class will be kept on waiting and the performance degrades at a high degree.

   1: public class Foo {

   2:     private Foo foo;

   3:     private Foo(){}

   4:     public static synchronized Foo getInstance(){

   5:        if(foo==null){

   6:          foo = new Foo();    

   7:         }

   8:         return foo;

   9:     }

  10: }

Double Checked Locking: In this method we will check whether the object is initialized before acquiring the lock, and again check whether any thread has completed the creation of the object after acquiring the lock, and then create an object.

   1: public class Foo{

   2:     private Foo foo;

   3:     private Foo() {}

   4:     public static foo getInstance(){

   5:         if(foo == null){

   6:             synchronized(foo){

   7:                 if(foo==null){

   8:                     foo = new Foo();

   9:                 }

  10:             }

  11:         }

  12:         return foo;

  13:     }

  14: }

In this approach, there is a limited possibility of error, that is when a Thread acquires a lock and a constructor is being called. Even before the completion of the constructor, another thread might request for getInstance and see that foo is not “null”, and hence returns a partially initialized object. These sort of partially initialized objects can break the system, and will be very hard to debug.

Double Checked Locking Using volatile: In this method we will be implementing the same Double Checked Locking pattern, while adding a keyword called volatile(Introduced in JDK 1.5) to the  object variable. Volatile keyword makes sure that there is no ThreadLocal copy of this variable, and hence will always reference to the same object though it is accessed by multiple threads.

   1: public Class Foo{

   2:     private volatile Foo foo;

   3:     private Foo(){}

   4:     public static foo getInstance(){

   5:         if(foo == null){

   6:             synchronized(foo){

   7:                 if(foo==null){

   8:                     foo = new Foo();

   9:                 }

  10:             }

  11:          }

  12:         return foo;

  13:     }

  14: }

Using Inner Class: In this method we use a Singleton Helper inner class to lazily load the object, which is guaranteed to have a single reference of it across all the threads.

   1: public class Foo{

   2:     private static class FooSingletonHelper{

   3:         private static Foo instance = new Foo();

   4:     }

   5:     private Foo(){]

   6:     public static Foo getInstance()[

   7:         return FooSingletonHelper.instance;

   8:     }

   9: }

I prefer, creating the Singleton objects using this Inner Class methods, since there is no need to explicitly handle the synchronization issues, and the job of creating and making sure that only instance of the object to the compiler.

Using Enums as Singletons:

Using Enum Singletons, is increasing in todays programming, as they are more Thread-safe, and again the job of creating an object is handled by the compiler itself. While we use the Enum’s, to mark types in java having some constants usage of enum as a singleton might not sound perfect for many reasons.

   1: public enum Foo{

   2:     Instance;

   3:     public void action(){

   4:         // perfrom an action

   5:     }

   6: }

 

Hope this article, has given some insights to you on Singleton pattern, and was a good read. The concepts, and examples I have given in the justification are based on my opinions and experience. I would really love to hear any comments or suggestions, so please give your feedback in the comments section.

References :

  • To know more about using Enums as Singletons, go through this link.
  • More about volatile keyword can be found here.
Advertisements

About Sharath

I am a passionate technologist, I have varied interests in Development, Testing and Maintenance phases of PDLC / SDLC. In my leisure I love to play with my daughter. I love eating out, hanging out with friends and reading blogs/articles.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s