Singleton: Design Pattern

Jul 20, 2020 By Puneet Verma

Singleton 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.

To implement a singleton class, you typically declare all of its constructors to be private and provide a static method that returns a reference to the single instance of the class. This instance is typically created when the class is first loaded and the reference is stored in a static field.

As the constructor is private, we can't directly create its object. The only way to use this class, you would call Singleton.getInstance() to get the single instance of the class.

To use the singleton, clients call the getInstance() method, which either returns the existing instance of the class if it has already been created or creates a new instance if it has not yet been created.

A Singleton design pattern is useful in situations where you need to ensure that only a single instance of a class exists in the application. This can be useful in the following cases:

  • A database connection pool: A singleton class could manage a pool of database connections, ensuring that there is only one instance of the pool and that connections are reused rather than created each time they are needed.
  • A logging class: A singleton class could manage a log file, ensuring that there is only one instance of the log and that log messages are written to the same file.
  • A configuration class: A singleton class could manage application-wide configuration settings, ensuring that there is only one instance of the configuration object and that it can be easily accessed by different parts of the application.
  • A thread pool: A singleton class could manage a pool of worker threads, ensuring that there is only one instance of the pool and that threads are reused rather than created each time they are needed.

 

It is important to note that singletons can introduce a global state into your application, which can make it more difficult to reason about and test. The use of singletons should be carefully considered and generally avoided in favour of dependency injection where possible.

To implement a singleton pattern in a class, you typically need to follow these steps:

  1. Declare a private constructor to prevent the class from being instantiated directly.
  2. Declare a static field to hold the single instance of the class.
  3. Provide a static method that returns the single instance of the class, creating it if necessary.

PHP example: 

class Singleton {
  private static $instance;

  private function __construct() {
    // private constructor to prevent direct instantiation
  }

  public static function getInstance() {
    if (self::$instance === null) {
      self::$instance = new self();
    }
    return self::$instance;
  }

  private function __clone() {
    // private __clone method to prevent cloning of instance
  }

  private function __wakeup() {
    // private __wakeup method to prevent unserialization of instance
  }
}

To use this class, you would call Singleton::getInstance() to get the single instance of the class.

It is important to note that the __construct, __clone, and __wakeup methods have been declared as private to prevent direct instantiation, cloning, and unserialization of the singleton instance. This ensures that there is only one instance of the class.

It is also a good practice to make the $instance field static so that it is shared across all instances of the class.