The Singleton is My Favourite Design Pattern

March 9th, 2008  | Tags:

I find it amazing the number of people that have studied computer science who do not know what the singleton design pattern is. It's so important when designing software I don't think I could create efficient code without it.

What it is

A singleton is a design pattern that forces the creation of only one instance of a class. That is, once a class has been instantiated, the running instance is referenced, not a new one.

How it works

The singleton is one of the rare times a private constructor is used. The reason for this is mentioned above, we only want one instance of the object so we don't allow access to the constructor. Instead, we will use a public static method to get the instance. I'll give the code here in PHP, it's actually quite simple.

Implementation

<?php
class MyClass {
 
  private static $instance;
 
  private function __construct(){
  }
 
  public static function getInstance(){
    if(self::$instance == null){
      self::$instance = new self;
    }
    return self::$instance;
  }
 
  public function method1(){
    //do some stuff
  }
 
  public function method2(){
    //do some more stuff
  }
}
?>

That is a full singleton implementation in PHP. Now, when you want to get the current object, it would look something like this:

<?php
$obj = MyClass::getInstance();
 
$obj->method1();
?>

The whole thing is very simple. All getInstance() does is creates a new instance of its own class if the variable $instance is null (meaning the class has not been instantiated yet), and if $instance is not null, it is just returned (we do not want to create another one!).

Why I love it

I've seen PHP scripts that totally do not know how to deal with database classes. Either they open and close connections as needed, or open connections when they're not needed and leave them open throughout the pages execution. Both are, in my opinion, not correct.

A database class should never have more than one instance of itself open. Only one instance (one connection) to the database should be made which ensures a minimal number of connections. You can see how implementing a singleton design pattern into a database class can be very beneficial.

You can see an example at the projects area that demonstrates the singleton with a database.

  1. October 9th, 2008 at 16:15
    Reply | Quote | #1

    Most of the time a singleton class is not needed. You can encapsulate and hide variables and functions using static class methods.

    Example:

    I usually use a static class like this to manage all database operations. There’s no need to make an instantiable object and then go to the trouble of making sure there’s never more than one. There are times when you must have a singleton object but they are rare — static classes are more useful and easier to deal with most of the time.

  2. October 9th, 2008 at 16:18
    Reply | Quote | #2

    Sigh. The code sample I tried to include inside a <code> block disappeared. Maybe you can fix it. Now I just get a duplicate post warning.

TOP