Skip to content

Instantly share code, notes, and snippets.

@bkrajendra
Created January 10, 2026 18:31
Show Gist options
  • Select an option

  • Save bkrajendra/ec6266f52381bd88ebb1db54bb44e895 to your computer and use it in GitHub Desktop.

Select an option

Save bkrajendra/ec6266f52381bd88ebb1db54bb44e895 to your computer and use it in GitHub Desktop.
singleton
class ConfigService {
private static instance: ConfigService;
private config = { apiUrl: 'https://api.example.com' };
private constructor() {} // // Private constructor prevents direct instantiation
static getInstance(): ConfigService {
if (!ConfigService.instance) {
ConfigService.instance = new ConfigService();
}
return ConfigService.instance;
}
getApiUrl() {
return this.config.apiUrl;
}
}
// Usage
const config1 = ConfigService.getInstance();
const config2 = ConfigService.getInstance(); // // true - same instance!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment