TypeScript Singleton Pattern Example

In this article, we will learn how to use and implement the Singleton Pattern in TypeScript with an example.
Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.

TypeScript Singleton Pattern Example

Let's create singleton.ts file and add the following code to it:
export class Singleton {
    // A variable that stores the singleton object. Initially,
    // the variable acts like a placeholder
    private static singleton: Singleton;

    // private constructor so that no instance is created
    private constructor() {
    }

    // This is how we create a singleton object
    public static getInstance(): Singleton {
        // check if an instance of the class is already created
        if (!Singleton.singleton) {
            // If not created create an instance of the class
            // store the instance in the variable
            Singleton.singleton = new Singleton();
        }
        // return the singleton object
        return Singleton.singleton;
    }
}

Usage

Let's create demo.ts file and add the following code to it:
import { Singleton } from "./singleton";

export function show() : void {
 const singleton1 = Singleton.getInstance();
 const singleton2 = Singleton.getInstance();

 if (singleton1 === singleton2) {
  console.log("two singletons are equivalent");
 } else {
  console.log("two singletons are not equivalent");
 }
}
show();
Run:
  • Compile the above code using the TypeScript compiler.
  • Above code is compiled to plan JavaScript code
  • Run Javascript code using node
design_patterns_in_typescript\singleton> tsc --target ES5 .\demo.ts
design_patterns_in_typescript\singleton> node .\demo.js
two singletons are equivalent

When to Use Singleton Pattern

Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
Use the Singleton pattern when you need stricter control over global variables.

All TypeScript Design Patterns

1. Creational Design Patterns

Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  1. TypeScript Singleton Pattern Example
  2. TypeScript Factory Design Pattern with Example
  3. TypeScript Abstract Factory Pattern Example
  4. TypeScript Builder Pattern Example
  5. TypeScript Prototype Pattern Example

2. Structural Design Patterns

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
  1. TypeScript Bridge Pattern Example
  2. TypeScript Adapter Pattern Example
  3. TypeScript Decorator Pattern Example
  4. TypeScript Composite Pattern Example
  5. TypeScript Flyweight Design Pattern Example
  6. TypeScript Facade Pattern Example
  7. TypeScript Proxy Pattern Example

3. Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
  1. TypeScript Command Pattern Example
  2. TypeScript Chain of Responsibility Pattern Example
  3. TypeScript Visitor Pattern Example
  4. TypeScript Template Method Pattern Example
  5. TypeScript Strategy Pattern Example
  6. TypeScript State Pattern Example
  7. TypeScript Observer Pattern Example
  8. TypeScript Memento Pattern Example
  9. TypeScript Mediator Pattern Example
  10. TypeScript Iterator Pattern Example
  11. TypeScript Interpreter Design Pattern Example

Comments