Design Patterns - Singleton


What is the purpose of the Singleton design pattern?

// Design Patterns - Singleton

If we need to guarantee that only one single instance of a class exists,
we would use the Singleton design pattern.

Some singleton that exists in Java:


The keys to implement a singleton:

1. have a private static instance of the same class.

2. the constructor should be private or protected

3. add a public static getInstance method which check to see if
    the private static instance has been already created.  If the private
    static instance is not already created, create an instance of this class
    and assign it to the static instance, and return it.  If the static instance
    has been already create, it simply returns this static instance.

4. Disable language-specific features using the private keyword.

class Singleton {
     // @var Singleton The reference to *Singleton* instance of this class
    private static $instance;

     // Returns the *Singleton* instance of this class.
     // @return Singleton The *Singleton* instance.
    public static function getInstance() {
        if (null === static::$instance) {
            static::$instance = new static();
        return static::$instance;

     // Protected constructor to prevent creating a new instance of the
     // *Singleton* via the `new` operator from outside of this class.
    protected function __construct() {

     // Private clone method to prevent cloning of the instance of the
     // *Singleton* instance.
     // @return void
    private function __clone() {

     // Private unserialize method to prevent unserializing of the *Singleton* instance.
     // @return void
    private function __wakeup() {

class SingletonChild extends Singleton {

$obj = Singleton::getInstance();
var_dump($obj === Singleton::getInstance()); // bool(true)

$anotherObj = SingletonChild::getInstance();
var_dump($anotherObj === Singleton::getInstance()); // bool(false)

var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)

In Java 7, it is very easy to create a singleton.  Just define the class as normal,
and then add the @Singleton annotation to the class.

The code above implements the singleton pattern using a static variable and the static creation method getInstance(). Note the following:

  1. The constructor __construct() is declared as protected to prevent creating a new instance outside of the class via the new operator.
  2. The magic method __clone() is declared as private to prevent cloning of an instance of the class via the clone operator.
  3. The magic method __wakeup() is declared as private to prevent unserializing of an instance of the class via the global function unserialize() .
  4. A new instance is created via late static binding in the static creation method getInstance() with the keyword static. This allows the subclassing of the class Singleton in the example.

The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire request lifecycle in a web application. This typically occurs when we have global objects (such as a Configuration class) or a shared resource (such as an event queue).

You should be wary when using the singleton pattern, as by its very nature it introduces global state into your application, reducing testability. In most cases, dependency injection can (and should) be used in place of a singleton class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our application, as the object using the shared or global resource requires no knowledge of a concretely defined class.

class Simple
    private static $cachedInstance = NULL;
    static function getInstance()
        if (! self :: $cachedInstance)
            self :: $cachedInstance = new Simple();

        return self :: $cachedInstance;

    private function __construct()
         // initialization code

How easy is it to create a Singleton with JEE7?

Very easy. We just have to define the class as a normal class. We should not have to define the private constructor. But we should probably do a bit more research on the @Singleton annotation in JEE7.

What are the characteristics of the singleton pattern?

  1. private constructor
  2. static instance
  3. the static getInstance method
  4. JEE7 has @Singleton annotation with @Startup, @PostConstruct, and @DependsOn("other bean") options.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License