Skip to main content

Singleton

CreationalGang of FourAbout 1 min

Intent

Ensure a class only has one instance, and provide a global point of access to it.

Explanation

Real-world example

There can only be one ivory tower where the wizards study their magic. The same enchanted ivory
tower is always used by the wizards. The ivory tower here is a singleton.

In plain words

Ensures that only one object of a particular class is ever created.

Wikipedia says

In software engineering, the singleton pattern is a software design pattern that restricts the
instantiation of a class to one object. This is useful when exactly one object is needed to
coordinate actions across the system.

Programmatic Example

Joshua Bloch, Effective Java 2nd Edition p.18

A single-element enum type is the best way to implement a singleton

public enum EnumIvoryTower {
  INSTANCE
}

Then in order to use:

    var enumIvoryTower1 = EnumIvoryTower.INSTANCE;
    var enumIvoryTower2 = EnumIvoryTower.INSTANCE;
    LOGGER.info("enumIvoryTower1={}", enumIvoryTower1);
    LOGGER.info("enumIvoryTower2={}", enumIvoryTower2);

The console output

enumIvoryTower1=com.iluwatar.singleton.EnumIvoryTower@1221555852
enumIvoryTower2=com.iluwatar.singleton.EnumIvoryTower@1221555852

Class diagram

alt text
Singleton pattern class diagram

Applicability

Use the Singleton pattern when

  • There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point
  • When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

Some typical use cases for the Singleton

  • The logging class
  • Managing a connection to a database
  • File manager

Known uses

Consequences

  • Violates Single Responsibility Principle (SRP) by controlling their creation and lifecycle.
  • Encourages using a globally shared instance which prevents an object and resources used by this object from being deallocated.
  • Creates tightly coupled code. The clients of the Singleton become difficult to test.
  • Makes it almost impossible to subclass a Singleton.

Credits