The constructor function type of the class to be turned into a Singleton.
The class constructor function.
A subclass extending the input class, equipped with a static instance getter.
Gets the shared, lazily-instantiated instance of the class.
Example demonstrating wrapping a class with Singleton and accessing its shared instance
class DatabaseConnection {
connect() {
console.log("Connected to DB");
}
}
const DbSingleton = Singleton(DatabaseConnection);
// Accessing the shared instance
const db1 = DbSingleton.instance;
const db2 = DbSingleton.instance;
console.log(db1 === db2); // true
A class decorator / mixin function that implements the Singleton design pattern.
It wraps the provided class constructor and returns a subclass that contains a static
instanceproperty, guaranteeing that only one instance of the class will be created and shared.