Singleton Design Pattern in Android Explained

Prachi Jamdade
3 min readAug 7, 2022

If you are an aspiring software developer, then you must know how to implement design patterns that you can use while developing software.

This article will introduce the most common design pattern in android which is a Singleton Design Pattern.

Let’s deep dive and understand how to implement singleton design patterns in Java and Kotlin.

Singleton Design Pattern

What are Design Patterns and why they are important to learn?

Design Patterns are reusable solutions to common software problems.

Design patterns usually deal with objects. They present a solution to a reoccurring problem that an object shows. And they are created to fix known problems.

In other words, they represent challenges, other developers already faced and prevent you from reinventing the wheel by showing you proven ways to solve those problems.

What is Singleton Design Pattern in Android?

The Singleton Pattern is a software design pattern which ensures that a class has only one instance and provides a global point of access to the object of that class. If we do multiple requests to the class, will get a single copy of an instance.

Let’s take a scenario to understand this -

  1. Once the user is logged into an app, the current user should be returned every time we access the user object.
  2. While implementing cache in your application. You don’t want multiple caches in your application. In that case, a singleton copy of the cache comes in handy.

Advantages of Singleton Pattern:

  • Beneficial for objects which need to be shared between different parts in your application.

For example, only a single copy of an instance will be required for Retrofit, Room DB, or Shared Preference.

  • Useful for resources that are expensive to create in your application.
  • And of course, handles memory efficiently.

Implement singleton objects in Java:

Most of the code is self-explanatory here. But I want to highlight synchronized keyword. If you omit this keyword from the getInstance() method declaration, you have to compromise with the thread safety.

If multiple running threads try to access getInstance() method, a new object will be created for the thread from which getInstance() method was called.

So if you want to avoid this type of scenario and want to make your code thread-safe, make getInstance() method synchronized.

Implement singleton objects in Kotlin:

Kotlin provides an easy way to create singletons using the object declaration feature. An object or a companion object is one singleton object in Kotlin.

As you can see here, init block is called only for the first time. Every time you access the class you will get the same instance of an object.

This way of creating singletons provided by Kotlin is thread safe as well. Even if multiple threads try to create an object, it will return the same object created beforehand.

Conclusion:

In this short blog, I have explained what design patterns are and why we use the Singleton pattern in our app. I also showed how you can implement singletons using Java and Kotlin.

I hope it will give you better clarity on singletons. You can follow me on twitter. Till then, Happy Learning.

--

--

Prachi Jamdade

GDSC Lead'23, Software Dev, I love to write about my learnings in tech and software development. You can find me on GitHub @Prachi-Jamdade