Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Singleton Method Design Pattern
Next article icon

Singleton Method Design Pattern in JavaScript

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Singleton Method or Singleton Design Pattern is a part of the Gang of Four design pattern and it is categorized under creational design patterns. It is one of the most simple design patterns in terms of modeling but on the other hand, this is one of the most controversial patterns in terms of complexity of usage.

Singleton-Method-Design-Pattern-in-java

Important Topics for the Singleton Method Design Pattern in JavaScript

  • What is Singleton Method Design Pattern in JavaScript?
  • When to use the Singleton Method Design Pattern in JavaScript?
  • Key Component of Singleton Method Design Pattern in JavaScript:
  • Implementation of Singleton Method Design Pattern in JavaScript
  • Use Case of Singleton Method Design Pattern in JavaScript
  • Advantage of Singleton Method Design Pattern in JavaScript:
  • Disadvantage Of Singleton Method Design Pattern in JavaScript

1. What is Singleton Method Design Pattern in JavaScript?

Singleton pattern is a design pattern which restricts a class to instantiate its multiple objects. It is nothing but a way of defining a class. Class is defined in such a way that only one instance of the class is created in the complete execution of a program or project.

2. When to use the Singleton Method Design Pattern in JavaScript?

Here are some situations in JavaScript where the Singleton pattern may be applicable:

  • Logging Service: For a logging service that needs to capture and centralize log entries across the entire application, a Singleton can ensure that there’s only one logging instance.
  • Managing Shared Resources: In scenarios where you need to manage shared resources like database connections, network connections, or thread pools, a Singleton can help coordinate and control access to these resources.
  • Service Classes: For services that should have a single instance, such as a data service, API service, or utility service, a Singleton pattern can provide a consistent interface.
  • Preventing Multiple Instantiations: When instantiating multiple instances of a class would cause issues or inefficiencies, a Singleton ensures that there is only one instance in the entire application.
  • Lazy Initialization: If the instantiation of an object is resource-intensive and you want to delay the creation until it is actually needed, a Singleton can provide lazy initialization.
  • Preventing Cloning: If you want to prevent the cloning of an object, which could lead to multiple instances, a Singleton can include logic to prohibit cloning.

3. Key Component of Singleton Method Design Pattern in JavaScript:

Key-Component-of-Singleton-Method-Design-Pattern-in-JavaScript

3.1. Private Constructor:

The class should have a private constructor to prevent instantiation of the class from external classes.

JavaScript
const Singleton = (function () {   // Private variable to store the instance   let instance; 

3.2. Private Static Instance:

  • The class should have a private static instance of itself. This instance is the single instance that the pattern ensures.
JavaScript
function Singleton() {     if (instance) {       // If an instance already exists, return that instance       return instance;     }      // Initialization code     // ...      // Assign `this` to the `instance` variable     instance = this;   } 

3.3. Public Static Method to Access the Instance:

  • The class provides a public static method that returns the instance of the class. If an instance does not exist, it creates one; otherwise, it returns the existing instance.
JavaScript
Singleton.prototype.getInstance = function () {     return instance;   }; 

3.4. Lazy Initialization:

  • The instance is created only when it is first requested. This is known as lazy initialization. It helps improve performance by avoiding the unnecessary creation of the instance.
JavaScript
Singleton.getInstance = function () {     if (!instance) {       // Lazy initialization: create the instance only when needed       instance = new LazySingleton();     }     return instance;   }; 

3.5. Prevention of Cloning and Serialization:

  • To maintain the Singleton property, you may need to override the clone method to throw an exception and ensure that the class is not serializable or provide custom serialization logic.
JavaScript
function revive(key, value) {     if (key === '' && value && value.__isSingleton) {       return instance;     }     return value;   } 

4. Implementation of Singleton Method Design Pattern in JavaScript

Problem Statement:

Implement a Singleton pattern in JavaScript, ensuring that only one instance of a class is created and providing a mechanism to access that instance. Additionally, prevent cloning and serialization of the Singleton instance.

Step wise explaination of above Problem Statement

Implementation-of-Singleton-Method-Design-Pattern

  • Immediately Invoked Function Expression (IIFE):The pattern uses an IIFE to create a closure, providing a private scope for the instance variable and functions within it.
  • Private Instance Variable: The instance variable is used to store the single instance of the Singleton class. It’s private to the closure, preventing direct access from outside.
  • Private Constructor: The Singleton constructor is defined inside the closure and checks if an instance already exists. If it does, it throws an error to prevent the creation of additional instances.
  • Static getInstance Method: The getInstance method is a static method of the Singleton class, providing a way to access the single instance. If the instance doesn’t exist, it creates a new one; otherwise, it returns the existing instance.
  • Clone and Deserialize Prevention: The clone and customDeserialize methods are defined to throw errors, preventing cloning and deserialization of the Singleton instance.
  • JSON Serialization Prevention: The toJSON method is overridden to return a JSON string with a marker property (__isSingleton). This marker is later used in the revive function to identify the Singleton instance during deserialization.
  • Reviver Function for Deserialization: The revive function is used as the reviver function for JSON.parse to check for the marker property and return the existing instance, preventing the creation of a new instance during deserialization.

Overall Code for Above Problem Statement in JavaScript:

JavaScript
const Singleton = (function () {  let instance;   function Singleton() {     if (instance) {       throw new Error("Singleton instance already exists. Use getInstance method.");     }     // Initialization code     this.data = Math.random(); // Example: Initialization with random data   }   Singleton.getInstance = function () {     if (!instance) {       instance = new Singleton();     }     return instance;   };   Singleton.prototype.clone = function () {     throw new Error("Cloning of singleton instance is not allowed.");   };   Singleton.prototype.customDeserialize = function () {     throw new Error("Deserialization of singleton instance is not allowed.");   };    // JSON.parse reviver function to prevent deserialization  function revive(key, value) {     if (key === '' && value && value.__isSingleton) {       return instance;     }     return value;   }   Singleton.prototype.toJSON = function () {     return JSON.stringify({ __isSingleton: true, data: this.data });   };    return Singleton; })();  // Usage try {   const singletonInstance1 = Singleton.getInstance();   console.log(singletonInstance1);    // Attempting to create another instance should throw an error   const singletonInstance2 = new Singleton(); } catch (error) {   console.error(error.message); // Singleton instance already exists. Use getInstance method. }  // Cloning prevention try {   const clonedInstance = Object.create(singletonInstance1);   console.log(clonedInstance); // Will throw an error } catch (error) {   console.error(error.message); // Cloning of singleton instance is not allowed. }  // Serialization prevention try {   const serializedInstance = JSON.stringify(singletonInstance1);   console.log(serializedInstance); // Will throw an error } catch (error) {   console.error(error.message); // Serialization of singleton instance is not allowed. }  // Deserialization prevention const jsonString = JSON.stringify(singletonInstance1); const deserializedInstance = JSON.parse(jsonString, revive); console.log(deserializedInstance); // Will be the same instance as singletonInstance1 

Output:

Singleton instance already exists. 
singletonInstance1 is not defined
singletonInstance1 is not defined

5. Use Case of Singleton Method Design Pattern in JavaScript

There is a application of singleton pattern like cache-memory, database connection, drivers, logging. Some major of them are :-

  • Hardware interface access: Singleton classes are also used to prevent concurrent access of class. Practically singleton can be used in case external hardware resource usage limitation required e.g. Hardware printers where the print spooler can be made a singleton to avoid multiple concurrent accesses and creating deadlock.
  • Logger : Singleton classes are used in log file generations. Log files are created by the logger class object.
    • Suppose an application where the logging utility has to produce one log file based on the messages received from the users.
    • If there is multiple client application using this logging utility class they might create multiple instances of this class and it can potentially cause issues during concurrent access to the same logger file.
  • Configuration File: This is another potential candidate for Singleton pattern because this has a performance benefit as it prevents multiple users to repeatedly access and read the configuration file or properties file. It creates a single instance of the configuration file which can be accessed by multiple calls concurrently as it will provide static config data loaded into in-memory objects.
  • Cache: We can use the cache as a singleton object as it can have a global point of reference and for all future calls to the cache object the client application will use the in-memory object.

6. Advantages of Singleton Method Design Pattern in JavaScript:

The Singleton method design pattern has several benefits:

  • Controlled access to sole instance: Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.
  • Reduced name space: The Singleton pattern is an improvement over global variables. It avoids polluting the name space with global variables that store sole instances.
  • Permits refinement of operations and representation: The Singleton class may be subclassed, and it’s easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.
  • Permits a variable number of instances: The pattern makesit easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.
  • More flexible than class operations: Another way to package a singleton’s functionality is to use class operations (that is,static member functions in C++ or class methods in Smalltalk).But both of these language techniques make it hard to change a design to allow more than one instance of a class.Moreover, static member functions inC++are never virtual, so subclasses can’t override them polymorphically.

7. Disadvantages Of Singleton Method Design Pattern in JavaScript

The Singleton Method Design Pattern have different disadvantage:

  • Concurrency Issues: If not implemented carefully, Singletons can introduce concurrency issues in multi-threaded applications. You may need to use synchronization mechanisms, like locks or mutexes, to ensure safe access to the Singleton instance, which can add complexity to your code.
  • Singleton Pattern Overuse: Due to its convenience, developers might overuse the Singleton pattern, leading to an abundance of Singleton instances in an application. This can defeat the purpose of the pattern and result in increased memory usage.
  • Initialization Overhead: Lazy initialization, while often an advantage, can introduce some overhead when the Singleton instance is first accessed. If the initialization process is resource-intensive, it can affect the application’s startup time.
  • Difficulties in Debugging: Debugging a Singleton-based codebase can be challenging, especially when issues related to the Singleton’s state arise. It can be hard to trace the source of problems when multiple parts of the code may have modified the Singleton’s data.
  • Limited Dependency Injection: Using dependency injection and inversion of control becomes less straightforward when relying on Singleton instances. It may be challenging to inject alternative implementations or configurations because the Singleton instance is typically accessed globally.


Next Article
Singleton Method Design Pattern

V

Vishal Garg
Improve
Article Tags :
  • Design Pattern
  • System Design
  • Functions
  • TCS-coding-questions
  • Volkswagen IT Services
Practice Tags :
  • Functions

Similar Reads

  • Software Design Patterns Tutorial
    Software design patterns are important tools developers, providing proven solutions to common problems encountered during software development. This article will act as tutorial to help you understand the concept of design patterns. Developers can create more robust, maintainable, and scalable softw
    9 min read
  • Complete Guide to Design Patterns
    Design patterns help in addressing the recurring issues in software design and provide a shared vocabulary for developers to communicate and collaborate effectively. They have been documented and refined over time by experienced developers and software architects. Important Topics for Guide to Desig
    11 min read
  • Types of Software Design Patterns
    Designing object-oriented software is hard, and designing reusable object-oriented software is even harder. Christopher Alexander says, "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way th
    9 min read
  • 1. Creational Design Patterns

    • Creational Design Patterns
      Creational Design Patterns focus on the process of object creation or problems related to object creation. They help in making a system independent of how its objects are created, composed, and represented. Creational patterns give a lot of flexibility in what gets created, who creates it, and how i
      4 min read
    • Types of Creational Patterns

      • Factory method Design Pattern
        The Factory Method Design Pattern is a creational design pattern that provides an interface for creating objects in a superclass, allowing subclasses to alter the type of objects that will be created. This pattern is particularly useful when the exact types of objects to be created may vary or need
        8 min read

      • Abstract Factory Pattern
        The Abstract Factory Pattern is one of the creational design patterns that provides an interface for creating families of related or dependent objects without specifying their concrete classes and implementation, in simpler terms the Abstract Factory Pattern is a way of organizing how you create gro
        8 min read

      • Singleton Method Design Pattern in JavaScript
        Singleton Method or Singleton Design Pattern is a part of the Gang of Four design pattern and it is categorized under creational design patterns. It is one of the most simple design patterns in terms of modeling but on the other hand, this is one of the most controversial patterns in terms of comple
        10 min read

      • Singleton Method Design Pattern
        The Singleton Method Design Pattern ensures a class has only one instance and provides a global access point to it. It’s ideal for scenarios requiring centralized control, like managing database connections or configuration settings. This article explores its principles, benefits, drawbacks, and bes
        11 min read

      • Prototype Design Pattern
        The Prototype Design Pattern is a creational pattern that enables the creation of new objects by copying an existing object. Prototype allows us to hide the complexity of making new instances from the client. The existing object acts as a prototype and contains the state of the object. Table of Cont
        8 min read

      • Builder Design Pattern
        The Builder Design Pattern is a creational pattern used in software design to construct a complex object step by step. It allows the construction of a product in a step-by-step manner, where the construction process can change based on the type of product being built. This pattern separates the cons
        9 min read

      2. Structural Design Patterns

      • Structural Design Patterns
        Structural Design Patterns are solutions in software design that focus on how classes and objects are organized to form larger, functional structures. These patterns help developers simplify relationships between objects, making code more efficient, flexible, and easy to maintain. By using structura
        7 min read
      • Types of Structural Patterns

        • Adapter Design Pattern
          One structural design pattern that enables the usage of an existing class's interface as an additional interface is the adapter design pattern. To make two incompatible interfaces function together, it serves as a bridge. This pattern involves a single class, the adapter, responsible for joining fun
          8 min read

        • Bridge Design Pattern
          The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern. There are 2 parts in Bridge design pattern : AbstractionImplementationThis is a design mechanism that encapsulates an implementation class inside of an interface class. The br
          4 min read

        • Composite Method | Software Design Pattern
          Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. The main idea behind the Composite Pattern is to build a tree structure of objects, where individual objects and composite objects share a common interface. T
          9 min read

        • Decorator Design Pattern
          The Decorator Design Pattern is a structural design pattern that allows behavior to be added to individual objects dynamically, without affecting the behavior of other objects from the same class. It involves creating a set of decorator classes that are used to wrap concrete components. Important To
          9 min read

        • Facade Method Design Pattern
          Facade Method Design Pattern is a part of the Gang of Four design patterns and it is categorized under Structural design patterns. Before we go into the details, visualize a structure. The house is the facade, it is visible to the outside world, but beneath it is a working system of pipes, cables, a
          8 min read

        • Flyweight Design Pattern
          The Flyweight design pattern is a structural pattern that optimizes memory usage by sharing a common state among multiple objects. It aims to reduce the number of objects created and to decrease memory footprint, which is particularly useful when dealing with a large number of similar objects. Table
          10 min read

        • Proxy Design Pattern
          The Proxy Design Pattern a structural design pattern is a way to use a placeholder object to control access to another object. Instead of interacting directly with the main object, the client talks to the proxy, which then manages the interaction. This is useful for things like controlling access, d
          9 min read

        3. Behvioural Design Patterns

        geeksforgeeks-footer-logo
        Corporate & Communications Address:
        A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
        Registered Address:
        K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
        GFG App on Play Store GFG App on App Store
        Advertise with us
        • Company
        • About Us
        • Legal
        • Privacy Policy
        • In Media
        • Contact Us
        • Advertise with us
        • GFG Corporate Solution
        • Placement Training Program
        • Languages
        • Python
        • Java
        • C++
        • PHP
        • GoLang
        • SQL
        • R Language
        • Android Tutorial
        • Tutorials Archive
        • DSA
        • Data Structures
        • Algorithms
        • DSA for Beginners
        • Basic DSA Problems
        • DSA Roadmap
        • Top 100 DSA Interview Problems
        • DSA Roadmap by Sandeep Jain
        • All Cheat Sheets
        • Data Science & ML
        • Data Science With Python
        • Data Science For Beginner
        • Machine Learning
        • ML Maths
        • Data Visualisation
        • Pandas
        • NumPy
        • NLP
        • Deep Learning
        • Web Technologies
        • HTML
        • CSS
        • JavaScript
        • TypeScript
        • ReactJS
        • NextJS
        • Bootstrap
        • Web Design
        • Python Tutorial
        • Python Programming Examples
        • Python Projects
        • Python Tkinter
        • Python Web Scraping
        • OpenCV Tutorial
        • Python Interview Question
        • Django
        • Computer Science
        • Operating Systems
        • Computer Network
        • Database Management System
        • Software Engineering
        • Digital Logic Design
        • Engineering Maths
        • Software Development
        • Software Testing
        • DevOps
        • Git
        • Linux
        • AWS
        • Docker
        • Kubernetes
        • Azure
        • GCP
        • DevOps Roadmap
        • System Design
        • High Level Design
        • Low Level Design
        • UML Diagrams
        • Interview Guide
        • Design Patterns
        • OOAD
        • System Design Bootcamp
        • Interview Questions
        • Inteview Preparation
        • Competitive Programming
        • Top DS or Algo for CP
        • Company-Wise Recruitment Process
        • Company-Wise Preparation
        • Aptitude Preparation
        • Puzzles
        • School Subjects
        • Mathematics
        • Physics
        • Chemistry
        • Biology
        • Social Science
        • English Grammar
        • Commerce
        • World GK
        • GeeksforGeeks Videos
        • DSA
        • Python
        • Java
        • C++
        • Web Development
        • Data Science
        • CS Subjects
        @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
        We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
        Lightbox
        Improvement
        Suggest Changes
        Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
        geeksforgeeks-suggest-icon
        Create Improvement
        Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
        geeksforgeeks-improvement-icon
        Suggest Changes
        min 4 words, max Words Limit:1000

        Thank You!

        Your suggestions are valuable to us.

        What kind of Experience do you want to share?

        Interview Experiences
        Admission Experiences
        Career Journeys
        Work Experiences
        Campus Experiences
        Competitive Exam Experiences