Singleton
CreationalEnsure only one instance of a class exists, and provide a global access point to it.
Like the President of a country — there is only one at a time, and everyone contacts the same person.
Global app configuration, logging service, database connection pool, cache manager.
📋 Code example (TypeScript)
// Node.js module-level singleton (modules are cached after first require)
class Logger {
private static instance: Logger;
private constructor() {}
static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}
log(msg: string) { console.log(`[LOG] ${msg}`); }
}
const logger = Logger.getInstance();