Creational Patterns  «Prev  Next»

Singleton Pattern as a Creational Pattern

The Singleton pattern ensures that a class has only one instance, and provides a global point of access to that class. It ensures that all objects that use an instance of this class use the same instance.
The figure below discusses the Singleton pattern.
Singleton pattern consisting of 1) private variable 2) private instance, and 3) getInstance() which returns a Singleton.
This graphic illustrates the **Singleton Design Pattern** using a class diagram. Below is a detailed breakdown of its components:
  1. Singleton Class:
    • The class is named Singleton.
    • It has one private static field, -instance: Singleton = null, which stores the single instance of the class.
    • It contains two methods:
      • Constructor: -Singleton(), which is private to prevent direct instantiation.
      • Instance Getter: +getInstance(): Singleton, which provides access to the single instance of the class.
  2. Factory Method:
    • The class diagram includes a dashed box labeled "Factory" that suggests the getInstance method acts as a factory, ensuring controlled creation of the Singleton instance.
  3. Access Modifiers:
    • Symbols for access modifiers are described in the diagram:
      • + for public.
      • - for private.
      • # for protected.
  4. Purpose:
    • The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.

Singleton pattern consisting of 1) private variable 2) private instance, and 3) getInstance() which returns a Singleton.
  1. Singleton Structure
  2. Singleton in Java - Example 1
  3. Singleton in Java - Example 2

Benefits of the Singleton Pattern

The following lists the benefits of using the Singleton pattern:
  1. Controlled access to sole instance.
  2. Reduced name space.
  3. Permits refinement of operations and representation.
  4. Permits a variable number of instances.
  5. More flexible than class operations.
When to use: You should use the Singleton pattern when: There must be exactly one instance of a class.
In software engineering, the Singleton Pattern is a 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.
The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
The term comes from the mathematical concept of a singleton.
There is criticism of the use of the singleton pattern, as some consider it an anti-pattern, judging that it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.
In C++ it also serves to isolate from the unpredictability of the order of dynamic initialization, returning control to the programmer.
Modeling Aggregation and Composition

SEMrush Software