Singleton design model implementation in JavaScript


When working in applications, it is imperative to design solutions that are maintainable and whose code is readable. You should be able to discover recurring issues in designs and then adopt a strategy to mitigate them. This way you can write much cleaner, better, and maintainable code. This is exactly where design patterns come in.

Design models provide solutions to problems that often arise in software engineering. Such solutions have proved their worth and have proved their worth. The Singleton design pattern is a Gang of Four (GOF) design pattern. It belongs to the category “creative design template“. Creative design patterns are those that deal with the creation of objects. There are two other categories of GOF design models, namely structural models and behavioral models.

In this series of articles, we will implement the GOF design patterns using JavaScript. Incidentally, JavaScript is a dynamic, weakly-typed scripting language that supports many of the features of OOP. This article, the first part of this series of design patterns articles, presents a discussion of how we can implement the Singleton design pattern using JavaScript.

What is the Singleton design pattern?

The Singleton design pattern is a creative pattern that indicates that one and only one instance of a class would persist in memory during the lifecycle of the application. In other words, this design pattern restricts the instantiation of a class to a single object. As far as JavaScript is concerned, a singleton is an instance that is only created once – any repeated call to the constructor always retrieves the same instance.

Singleton model implementation

An easy way to implement the Singleton design pattern in JavaScript is to use an object literal, as shown below.

var singleton = {
  method1: function () {
    //Write your usual code here
     method2: function () {
     //Write your usual code here 

Here’s another code snippet that demonstrates how you can implement the Singleton pattern in JavaScript.

var Singleton = (function(){
    function Singleton() {
        //Write your usual code here
    var instance;
    return {
        getInstance: function(){
            if (null == instance) {
                instance = new Singleton();               
                instance.constructor = null; // Note how the constructor is hidden to prevent instantiation
            return instance; //return the singleton instance

Refer to the code snippet above. Note how a check is performed to see if the instance is null. If the instance is null, a new instance is created and returned by removing / hiding the constructor. If the instance is not NULL, the instance is returned. This approach is also known as the module model. As you can see, you can wrap the private members that belong to a particular instance using closures.

Now that we have implemented our Singleton class, the following code snippet can be used to call the function.

var justOneInstance = Singleton.getInstance();

The GOF states: “The Singleton model limits the number of instances of a particular object to one. This single instance is called the singleton. Singletons are useful in situations where system-wide actions need to be coordinated from a single central location. An example is a database connection pool. The pool manages the creation, destruction, and lifetime of all database connections for the entire application, ensuring that no connections are “lost”.

Summary This article provided a discussion of the Singleton design pattern, why it is useful, and how it can be implemented in JavaScript with sample code where appropriate. In the next articles in this series, we’ll explore the other design patterns and look at how they can be implemented in JavaScript. Good reading!



Leave A Reply