Design Patterns in 1 minute: Singleton #4

Classification: Creational

The Singleton Design Pattern ensures that a class has a single (single) instance shared across the entire application.

The Problem

Imagine you need to share the same state of an object throughout the entire application — and this state can change during program execution.

How can we solve this problem?

The Solution

The idea here is for the class itself to manage the creation of the object. For that, the class should store its instance in a static property (1), provide a method that acts as a constructor/accessor for the instance (2), ensuring that the same instance is always returned.

Also make the class constructors private (3).

public class MySingleton {

  // 1
  private static MySingleton INSTANCE;

  // 3
  private MySingleton() {
  }

  // 2
  public static MySingleton getInstance() {
    if (INSTANCE == null) {
      INSTANCE = new MySingleton();
    }
    return INSTANCE;
  }

}

Source Code

Go Deeper