Singleton Design Pattern

Ayush Khare
3 min readSep 25, 2016

Singleton Design Pattern is a simple design pattern in Java. The name Singleton says everything about itself. This is the best way to create an object for a class. So to be more clear, “This pattern involves a single class which is responsible for creating an object while making sure that only one object gets created.”

So before we begin, I am assuming that you have the basic knowledge of Android and Object Oriented Programming.

Let’s have a look at a very simple piece of code below:

Let’s take a walkthrough so that things become more clear.

  • Firstly, we have a private constructor. So what does it do? Why do we make it private? Pretty simple, so that our Singleton class cannot be instantiated by any other class.
  • Secondly, we have a static variable sInstance (following the naming convention), which contains the reference to our Singleton class, along with the synchronized keyword.

Note: Why is synchronized keyword important here? It prevents different threads from creating different instances of our Singleton class. Consider a scenario where we have two separate threads,

What if threadA hangs at

And threadB executes, creating an instance for itself. Now when threadA resumes, it proceeds to create another object for itself. So to overcome this problem of multiple threads creating multiple objects, we use the synchronized keyword. You can read more about it here.

  • Then, we have a static method which employs a technique known as lazy instantiation. What it means is singleton instance is not created unless this method is called for the very first time.
  • Lastly, we have a getter and setter method.

Now, Let’s create the activity classes to see how this pattern is used.

So what we have now are two Activities, namely ActivityA and ActivityB

Let’s take a walkthrough again for the above two pieces of code:

  • ActivityA consists of a single button and a Toast message which displays the value of the String we have in our Singleton class.
  • We use the getInstance() method of our Singleton class to initialise our Singleton object.
  • Finally, we have the onClick() method which sends the user from ActivityA to ActivityB using the Intent class.
  • ActivityB also uses the getInstance() method to initialise another object of our Singleton class.
  • The singleton object is now used to call the setter method and pass a String value, which overrides the previous default value that was set in our Singleton class.

Note: We have one single singleton object per class, as per the guidelines.

Let’s see what happens when we execute this code:

So, for the first time when ActivityA launches we can see the “Hello” Toast message whose value was set by the private constructor of Singleton class.

When we use the button to switch from ActivityA to ActivityB, we exclusively set the value of the String using the setString() method, and as a result ActivityB displays a Toast which reads the value we defined.

Now, what happens if we initialise another Singleton object in ActivityB using the getInstance() method?

The Answer is simple, we get the values stored in the latest object we define.

Another interesting point to note: As long as the process is not killed by the OS, if you try to reopen your app, you can see the Toast message with “World!” displaying on both the screens. Why? The same reason, the latest defined object remains in the memory.

To go in to further details and explore other useful examples, you can checkout Simple Singleton Pattern in Android, Singleton Design Pattern and Singleton in Android.

--

--