Const
class ExampleSingleton {
private static _count = 0;
private readonly _id: number;
public constructor() {
ExampleSingleton._count += 1;
this._id = ExampleSingleton._count;
console.log(`ExampleSingleton created with ID: ${this._id}`);
}
public sayHello(): void {
console.log(`Hello from instance ${this._id}!`);
}
}
SingletonManager.register('ExampleSingleton', ExampleSingleton);
SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello(); // Output: ExampleSingleton created with ID: 1 /n Hello from instance 1!
SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello(); // Output: Hello from instance 1!
SingletonManager is an instance of the SingletonManagerSingleton class that manages the singletons in the application. When a class is registered, the SingletonManagerSingleton creates a new instance of the class when it is requested.