-
Notifications
You must be signed in to change notification settings - Fork 0
Singleton Design Pattern
D. Hristov edited this page Sep 19, 2023
·
2 revisions
A Singleton is a creational design pattern that lets us ensure that a class has only one instance and at the same time it provides a global access point to this instance.
- We need a kind of global state
- Cross-component sharing data
- Single database connection within the entire app
- Config manager
etc...
export default class Singleton {
private static instance: Singleton;
private constructor() {}
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
return Singleton.instance;
}
// Rest of the logic starts from here....
}
More info can be found on the wiki page.