JavaOmnibus
GoF Singleton
GoF pattern • Creational
GoFCreationalJava example

Singleton solves a specific structural problem in the design.

Ensure only one instance exists for a shared service or coordinator.

Pattern overview

GoF pattern

Ensure only one instance exists for a shared service or coordinator.

Intent

Guarantee a class has only one instance and provide a global access point to it.

Problem

A system needs exactly one shared coordinator, cache, registry, or configuration holder.

Solution

Control instantiation privately and expose a single well-known instance.

Fit and tradeoffs

FamilyGoF
CategoryCreational
Best used when
  • When there truly should be one instance per JVM or context.
  • When you need a stateless utility with lifecycle control.
Tradeoffs
  • Can hide dependencies and make testing harder.
  • Often overused where dependency injection would be clearer.

Java example

This example is intentionally compact so the pattern shape stays easy to see.

public final class AppClock {
    private static final AppClock INSTANCE = new AppClock();

    private AppClock() {}

    public static AppClock getInstance() {
        return INSTANCE;
    }

    public Instant now() {
        return Instant.now();
    }
}

Related pages

Keep exploring