Observer & Singleton Patterns
What are the best practices for implementing a Singleton in a module system?
// singleton.js
let instance = null;
export class ModuleSingleton {
constructor() {
if (instance) {
return instance;
}
instance = this;
this.initialize();
}
initialize() {
this.config = {};
}
}
export const getInstance = () => {
if (!instance) {
instance = new ModuleSingleton();
}
return instance;
};
// Usage:
// import { getInstance } from './singleton.js';
// const singleton = getInstance();