Skip to main content

TypeScript Example

/**
* Singleton Design Pattern
*
* Intent: Lets you ensure that a class has only one instance, while providing a
* global access point to this instance.
*/

/**
* The Singleton class defines the `getInstance` method that lets clients access
* the unique singleton instance.
*/
class Singleton {
private static instance: Singleton;

/**
* The Singleton's constructor should always be private to prevent direct
* construction calls with the `new` operator.
*/
private constructor() {}

/**
* The static method that controls the access to the singleton instance.
*
* This implementation let you subclass the Singleton class while keeping
* just one instance of each subclass around.
*/
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}

return Singleton.instance;
}

/**
* Finally, any singleton should define some business logic, which can be
* executed on its instance.
*/
public someBusinessLogic() {
// ...
}
}
/**
* The client code.
*/
function clientCode() {
const s1 = Singleton.getInstance();
const s2 = Singleton.getInstance();

if (s1 === s2) {
console.log("Singleton works, both variables contain the same instance.");
} else {
console.log("Singleton failed, variables contain different instances.");
}
}

clientCode();
Singleton works, both variables contain the same instance.