Do you really understand?

The Singleton Design Pattern

Helps you not compromise performance | Day 8 of #100DaysOfCode

Dhananjay Trivedi
3 min readSep 25, 2019

Singleton is a creational design pattern that ensures you have only one instance while providing a single point of access to this instance. It violates the Single Responsibility Principle.

Technical Example, There will be times that you might want to control the access of resources by your program. This could be due to limited resources which can cause performance issues of your program, or maybe at times because of the business logic.

You can achieve this functionality without letting your client objects knowing about it. Whenever they request access to some limited resources, you create only one instance of it and return the same instance every time someone requires it. Checking for the availability of the resources and then giving access to it is the additional logic you have to worry about. Genius!

That’s not all!

Remember those global variables that you (alright me!) use to store some essential objects references? Well, that’s a bad practice to do as they are accessible from anywhere in the program and any part of the program can mutate them which can lead to bugs and crashes.

Singleton pattern lets you access those objects from anywhere in the program at the same time providing the protection against mutation from anywhere in the program.

Alright! How to implement Singleton?

Two things to keep in mind,

  1. There should be no public default constructor so as to prevent any other class to create an object of the same.
  2. There should be a static method that acts as a single point of contact with the class and returns you the instance of the class. This “proxy-constructor” will call the default private constructor and save its reference in a static field at the first invocation.
public class MyClass {     // Static reference to store the reference to the instance
private static final MyClass myClass = new MyClass();

// Private default constructor
private MyClass() {}
// Our proxy constructor which returns the static reference
public static MyClass getInstance() {
return myClass;
// You can add your own logical checks
// To restrict the access as you may like
}
}

Alright Cool, Where to use Singletons?

Singleton is so popular nowadays that people almost apply this everywhere for solving the smallest of the problems. Apply singleton when you need to:

  • Restrict sharing of only a single instance of a class
  • Have strict control over the variables of the class
  • Initializing an object of a class is an expensive operation which you don’t want to do again and again.
  • Singleton assures that a single instance of a class exists at all times.

Any drawbacks?

  • It compromises the Single Responsibility Principle.

The single responsibility principle (SRP) states that every class or module in a program should have responsibility for just a single piece of that program’s functionality.

Here the common instance will be shared by multiple classes and objects for doing multiple different operations.

  • Can lead to high coupling in your code, hence may lead to bad software design.
  • Requires more attention in multi-threading environment as multiple threads can concurrently invoke the function for the first time.

That’s for the 8th-day folks! I hope you are learning along with me with this series. Let me know your thoughts, or share any feedback so that I can bring more value to you. See you tommorrow!

I am Dhananjay Trivedi a Full-Stack Design-Minded Android Developer, an AR Developer & ML Enthusiast. Or at least, that’s what I like to think of myself currently.

Currently working at Softway empowering the mobile application development team along with the Machine Learning Researchers team. Learn more about Softway:

--

--