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.
- TypeScript Singleton Pattern Example
- TypeScript Factory Design Pattern with Example
- TypeScript Abstract Factory Pattern Example
- TypeScript Builder Pattern Example
- 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.
- TypeScript Bridge Pattern Example
- TypeScript Adapter Pattern Example
- TypeScript Decorator Pattern Example
- TypeScript Composite Pattern Example
- TypeScript Flyweight Design Pattern Example
- TypeScript Facade Pattern Example
- TypeScript Proxy Pattern Example
3. Behavioral Design Patterns
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
- TypeScript Command Pattern Example
- TypeScript Chain of Responsibility Pattern Example
- TypeScript Visitor Pattern Example
- TypeScript Template Method Pattern Example
- TypeScript Strategy Pattern Example
- TypeScript State Pattern Example
- TypeScript Observer Pattern Example
- TypeScript Memento Pattern Example
- TypeScript Mediator Pattern Example
- TypeScript Iterator Pattern Example
- TypeScript Interpreter Design Pattern Example
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course