At first glance, on the software world, singleton its’ a design pattern that means one unique instance only.

By refactoring guru the word intent is to be:

A creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.

The word singleton comes from math, which from wikipedia states:

A singleton (also known as a unit set[1] or one-point set) is a set with exactly one element.

Singleton word, it’s a mix of two smaller ones, single and ton. Single word is pretty explanatory and -ton is used in surnames that come from place names, such as Newton, Hilton, Dayton, and Clinton.

But why would you use a singleton in your project?

  1. First advantage of using it is the fact that you can have it as a place for your global variables or any type of global access. Instead of having to re-instantiate or look for properties or any other types of data source every time you want to reach for it (degrading I/O performance), if you load into a singleton object instance, that object will always be available for the whole application, as a single source of truth.

  2. Second advantage is that you don’t need / can’t have multiple objects for the same end if just one is enough for it, having more than one you just cause you to increase memory for no big reason and breaks the idea of singleton (unique instance).

What problems comes with singleton?

  1. It makes harder to test your code since uses static (but this can be solved with dependency injection or usage of power mock).

  2. It brakes single responsibility principles (solves 2 items — the advantages).

  3. Since it’s easy to share data/functionalities global, users tend to abuse it breaking it’s purpose (e.g. having multiple functionalities that are not made for the same end), so the team needs to be aware of it and use with caution.

One extra tip is to be aware of multi thread usage/access and how this can affect your singleton and related arguments if you make changes on attributes that are shared between the threads.

How can you create a singleton in Java?

  1. You want a unique reference of your object, to do that you use static keyword.

  2. You don’t want to have anyone to instance it, so you add a private constructor and make the instance in the class itself.

  3. One last thing to be aware is that you can have the instance created in a static way (as soon as the application get’s started) or once someone requests a instance of it.

With those two ideas in place the result in a lazy instance is:

// Lazy instance
public class SingletonExample {

    public static SingletonExample instance;

    private SingletonExample() {
      // private constructor
    }

    public SingletonExample getInstance() {
        if (instance == null) {
            instance = new SingletonExample();
        }
        return instance;
    }
}

If you want an eager approach would be:

// Eager instance
public class SingletonExample {

    public static SingletonExample instance = new SingletonExample();

    private SingletonExample() {
      // private constructor
    }

    public SingletonExample getInstance() {
        return instance;
    }
}

Today singleton is the most used design pattern in the software industry I would say, it’s well adopted and used everywhere.

That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)