Singleton pattern is a convention lets us enforce creation of only one instance for a class in a clean manner.
It is used for a lot of usecases:
Singleton Class has the following structure
class Singleton {
/**
* Static variable to hold the one instance of the class Singleton
*/
private static uniqueInstance: Singleton;
/**
* Our constructor is declared private;
* only Singleton can instantiate this class!
*/
private constructor() {}
/**
* The getInstance() method gives us a way to instantiate
* the class and also to return an instance of it
*/
public static getInstance(): Singleton {
if (!this.uniqueInstance) {
this.uniqueInstance = new Singleton();
}
return this.uniqueInstance;
}
// other useful methods here
}
class Singleton {
private static uniqueInstance = new Singleton();
// ...
}