188
GOF Creational

Singleton

Reference Wikipedia ↗

Singleton design pattern enables us to guarantee unicity and a single entry point for services and accesses. Tipically it requires the standard contructor to be hidden in order to avoid direct instantiation, but modern frameworks use this pattern in conjunction with other creational patterns in order to ease the development of services.

Usage

Typical usage for the Singleton pattern is the management of a single centralized state with the warranty that nothing that is not part of the singleton interface will touch it.

Examples

  • Angular services are (per default) singletons. These services are instantiated by the framework and can be used directly through Dependency Injection.

Specimens

15 implementations
Specimen 188.01 Dart View specimen ↗

The Singleton pattern ensures a class has only one instance, and provides a global point of access to it. This is useful for managing resources like database connections, configuration settings, or logging services where multiple instances would be problematic or inefficient. In Dart, this is efficiently implemented using a static instance and a private constructor. The private constructor prevents external instantiation, while the static factory method instance provides controlled access to the single instance. This approach leverages Dart’s static members and constructor control for a concise and thread-safe Singleton.

/// A class representing a Singleton.
class Singleton {
  // Private constructor to prevent direct instantiation.
  Singleton._();

  // Static instance to hold the single instance.
  static final Singleton _instance = Singleton._();

  // Static factory method to access the instance.
  static Singleton get instance => _instance;

  // Example data/method to demonstrate usage.
  String data = 'Initial Data';

  void updateData(String newData) {
    data = newData;
  }
}

// Example Usage (not part of the Singleton class itself)
void main() {
  final singleton1 = Singleton.instance;
  final singleton2 = Singleton.instance;

  print(singleton1 == singleton2); // Output: true

  singleton1.updateData('Updated Data');
  print(singleton2.data); // Output: Updated Data
}