Skip to content

Singleton Pattern in JavaScript

The Singleton Pattern is a design pattern that ensures a class has only one instance and provides a global point of access to that instance. It is often used to manage shared resources, like a configuration object, logging service, or database connection. The key feature of the Singleton is that it restricts the instantiation of a class to a single object, meaning no matter how many times you try to create a new instance, only the first instance will be used.


Table of Contents

  1. What is the Singleton Pattern?
  2. Singleton Pattern Syntax Example
  3. When to Use the Singleton Pattern
  4. Trade-offs of the Singleton Pattern
  5. Conclusion

1. What is the Singleton Pattern?

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to that instance. It’s commonly used in situations where you need to control access to shared resources, such as:

  • Database connections
  • Logging systems
  • Configuration settings

In the Singleton Pattern, a class creates and manages a single instance, and any subsequent attempts to create new instances will return the same object.


2. Singleton Pattern Syntax Example

Let’s take a look at a simple example of how the Singleton Pattern works. We'll create a Logger class that ensures only one instance of the logger is used throughout the application.

Example: Logger Singleton

javascript
class Logger {
  static instance = null;
  constructor() {
    if (Logger.instance) {
      return Logger.instance; 
    }
    Logger.instance = this; 
  }
  log(message) {
    console.log(message);
  }
}

// Usage
const logger1 = new Logger();
const logger2 = new Logger();

logger1.log("This is a log message.");
logger2.log("This is another log message.");

console.log(logger1 === logger2);  // true, both are the same instance

Breakdown of the Example:

  1. Static Property: The static property instance is used to store the single instance of the Logger class.
  2. Private Constructor: The constructor checks if an instance of Logger already exists. If it does, it returns the existing instance. If not, it creates and stores the new instance.
  3. Public Method: The log method is used to log messages to the console.
  4. Usage: When two instances (logger1 and logger2) are created, they both point to the same instance. This is verified by the console.log(logger1 === logger2) statement, which returns true.

3. When to Use the Singleton Pattern

The Singleton Pattern is ideal in situations where:

  • You want to ensure only one instance of a class is created.
  • You need global access to that single instance (e.g., for logging, configuration settings, or shared resources).
  • Creating multiple instances of a class would be inefficient or unnecessary (e.g., database connections, cache objects).

4. Trade-offs of the Singleton Pattern

Trade-offDescription
Controlled AccessThe Singleton provides a controlled point of access to a shared resource, ensuring that there’s only one instance.
Global AccessThe Singleton makes the instance globally accessible, which can be useful for managing shared resources across the application.
Lazy InitializationThe instance is only created when it is needed, which can improve performance.
Reduced Memory UsageSince there is only one instance, it reduces memory usage compared to creating multiple instances of the same class.
Hard to TestThe Singleton pattern provides global access, making it challenging to isolate it for unit testing.
Hidden DependenciesOther parts of your application may unknowingly depend on the Singleton, leading to tight coupling between components.
Global StateThe Singleton introduces global state, which can lead to unpredictable behavior, especially in multi-threaded environments.
Difficult to ExtendThe Singleton pattern can make it hard to extend or modify the class since you’re restricted to a single instance.

5. Conclusion

The Singleton Pattern is a valuable design pattern when you need to ensure that only one instance of a class exists and provide global access to that instance. It’s especially useful for managing shared resources like configuration settings, logging, or database connections.

However, it’s important to use the Singleton Pattern carefully, as it introduces global state and can create challenges in testing and maintaining dependencies. In large-scale applications, the Singleton Pattern should be applied when it truly makes sense to have a single point of access to a resource, rather than using it indiscriminately.

When used correctly, the Singleton Pattern can help you manage resources efficiently and maintain cleaner, more organized code.


Additional Resources

Made By Sourav Bandyopadhyay