creational
Singleton
Ensure a class has only one instance and provide a global access point to it.
Problem
A system needs exactly one shared instance of a class, and that instance must be globally accessible without allowing uncontrolled duplication.
Integration notes
Singleton is useful for centralized services like configuration, logging, caching, and shared application state.
Tradeoffs
- Can make dependencies harder to test if the singleton is used everywhere
- May introduce hidden global state that complicates debugging and concurrency
Languages
typescript, java, python, angular, react, react-native, csharp, dotnet
Platforms
Web, Backend, Mobile, UI kits, Cross-platform systems
Variants
Scenarios
Application config singleton
A shared configuration object loads environment settings once and serves them throughout the application.
Language examples
Core languages
Frameworks and libraries
Application config singletontypescript
The config singleton ensures the application reads one shared settings instance instead of creating a new configuration object everywhere.
TypeScript
class AppConfig {
private static instance: AppConfig;
private constructor(
public readonly appName: string,
public readonly environment: string,
) {}
static getInstance(): AppConfig {
if (!AppConfig.instance) {
AppConfig.instance = new AppConfig("Atlas App", "production");
}
return AppConfig.instance;
}
}
const configA = AppConfig.getInstance();
const configB = AppConfig.getInstance();
console.log(configA === configB);
console.log(configA.appName);Real-world examples
- Configuration services: Applications often use one shared config source for environment variables, feature flags, and runtime settings.
- Central logging pipelines: Many systems route logs through one shared logger or telemetry client to keep formatting and destination consistent.
- Shared caches: Apps frequently maintain one cache instance so repeated lookups can reuse the same stored values.