Singleton Pattern — Thread-Safe Approaches
Implement thread-safe singletons in Java: enum, holder class, double-checked locking, and eager init.
// 1. BEST: Enum singleton — simplest, thread-safe, serialization-safe
public enum ConfigManager {
INSTANCE;
private final Map<String, String> config = new HashMap<>();
public void set(String key, String value) { config.put(key, value); }
public String get(String key) { return config.getOrDefault(key, ""); }
}
// Usage: ConfigManager.INSTANCE.get("db.url");
// 2. Static holder (lazy, thread-safe without synchronization)
public class DatabasePool {
private DatabasePool() {
// initialize pool
}
private static class Holder {
static final DatabasePool INSTANCE = new DatabasePool();
}
public static DatabasePool getInstance() {
return Holder.INSTANCE;
}
}
// 3. Double-checked locking (when you need constructor args)
public class CacheService {
private static volatile CacheService instance;
private final int maxSize;
private CacheService(int maxSize) {
this.maxSize = maxSize;
}
public static CacheService getInstance(int maxSize) {
if (instance == null) {
synchronized (CacheService.class) {
if (instance == null) {
instance = new CacheService(maxSize);
}
}
}
return instance;
}
}Use Cases
- Application-wide configuration managers
- Database connection pool instances
- Shared cache and resource management
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Builder Pattern — Fluent Object Construction
Implement the Builder pattern for complex objects with validation, immutability, and method chaining.
Best for: Constructing complex objects with many optional parameters
Strategy Pattern with Lambdas
Implement the Strategy pattern using interfaces and Java lambdas for flexible algorithm selection.
Best for: Swappable pricing or discount algorithms
Factory Method Pattern with Registry
Implement the Factory pattern using a registry map for extensible object creation without switch statements.
Best for: Extensible object creation in plugin architectures
Observer Pattern — Event System
Build a type-safe event system using the Observer pattern with generics and functional interfaces.
Best for: Decoupled event-driven architectures