Singleton 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. Singleton pattern is used to implement the mathematical concept of a singleton, by restricting 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 (say, five).

UML Representation


The following lists the benefits of using the Singleton pattern:
  • Controlled access to sole instance
  • Reduced name space
  • Permits refinement of operations and representation
  • Permits a variable number of instances
  • More flexible than class operations

When to Use

You should use the Singleton pattern when:
  • There must be exactly one instance of a class.