Thursday, May 3, 2012

JavaScript Fun Part 4: The Singleton Pattern

What Is The Singleton Pattern?

This is a pattern by which you allow one and only one instance of an instanced object to be created for use. Generally, this object is created in a "lazy" fashion. That is to say, there is no instance created until it's requested the first time. Examples of where this pattern would be used on the client are few and far between, I suppose, but with the advent of JavaScript server technologies like NodeJS, I'm sure that the JavaScript singleton pattern will start to find a lot more use.

Implementing The Singleton Pattern In JavaScript

I have seen a few ways this pattern has been implemented in JavaScript. All of them work pretty much the same way with a containing function called immediately, so as to allow a private "instance" of the class in question. The following is my favorite way to implement this pattern:

var getSingleton = (function() {
    //this will hold our one instance.
    var instance = null;

    //here is the class declaration, all private n stuff.
    var Singleton = function() { = function() { 
            alert(' called.');

    //we return a function to create and get the one instance.
    return function() {
        //create the one instance if necessary.
       instance = instance || new Singleton();
       return instance;

var x1 = getSingleton();
var x2 = getSingleton();
alert(x1 === x2); //true

try {
    var fail = new Singleton();
}catch(err) {
    alert(err); //Reference Error: Singleton is not defined.

I start off by creating the containing function that executes immediately and returns its result to the getSingleton variable. The containing function as a private variable for storing our singleton instance, as well as a private declaration for our function class, Singleton. Because Singleton is declared inside the closure of our containing function, it can't be accessed anywhere else. Now we move in for the kill: Our containing function returns a function reference that does the following: Checks to see if our instance is null, populates it with a new instance of our Singleton class if so, then returns the instance. Now getSingleton has a function reference in it that will get our one instance of the Singleton class, creating it only if necessary.

EDIT: Another approach! 

I just wanted to show another approach for a Singleton class that has the advantage of one less closure. In fact, I think I prefer this method to the other. More closures means more memory usage (however small), so I think going forward this will be my most likely singleton implementation.

var getSingleton2 = (function() {
    //this will hold our instance.
    var instance = null;

    //private methods to the singleton go here.
    var privateMethod = function() {
    //return a method that gets the instance.
    return function() {
        instance = instance || {
            //declare public members here.
            foo: function() {

var s1 = getSingleton2();
var s2 = getSingleton2();
alert(s1 === s2);​ //true​​​​

No comments:

Post a Comment

This form allows some basic HTML. It will only create links if you wrap the URL in an anchor tag (Sorry, it's the Blogger default)