You are here: Home / Topics / Singleton Design Pattern - TypeScript

Singleton Design Pattern - TypeScript

Filed under: TypeScript on 2024-03-14 17:15:36

The Singleton pattern can be useful in various scenarios where you need to ensure that there is only one instance of a class and provide global access to that instance. Some real-time use cases of the Singleton pattern include:

Database Connections: When working with a database, you may want to have a single instance of a database connection that can be shared across different parts of the application. Using a Singleton, you can ensure that only one connection is established, and multiple components can access and utilize it efficiently. 

Logging: In a logging system, you may want to have a single logger instance that collects and records logs from different parts of the application. By implementing the Singleton pattern, you can ensure that all logging calls go through the same logger instance, enabling centralized logging functionality.

Configuration Settings: If you have application-wide configuration settings, such as database credentials, API keys, or other global configurations, you can use a Singleton to store and access these settings from anywhere in the application.

Caching: When implementing a caching system, you might want to have a single cache instance shared across different components to store frequently accessed data. The Singleton pattern ensures that all components use the same cache instance, avoiding duplication of data and improving performance.

State Management: In certain cases, you may need to manage an application state that should be accessible from multiple components. By using a Singleton pattern, you can create a central state management object that holds the application's current state and provides a consistent view of the state to all components.

These are just a few examples of how the Singleton pattern can be used in real-world applications. The key idea is to have a single instance that provides global access and ensures consistency and coordination across different parts of the system. However, it's essential to use the Singleton pattern judiciously and be mindful of its potential limitations, such as potential tight coupling and difficulties in testing.


Example 1: 
class Singleton {
private static instance: Singleton;
private constructor() {
// Private constructor to prevent direct instantiation
console.log("Constructor Invoke");
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
console.log("Instance Created");
return Singleton.instance;
public someMethod(): void {
console.log("Some method called.");
const instance1 = Singleton.getInstance();
const instance2 = Singleton.getInstance();
console.log("instance1 === instance2: " + (instance1 ===
instance2)); // Output: true
instance1.someMethod(); // Output: Some method called.
instance2.someMethod(); // Output: Some method called.
Note: compile this file using either tsc index.ts or tsc index.ts – target es2015 & then node index.js

About Author:
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.