singleton
Ensure a class has only one instance and provide a global access point to it.
Files1
View on GitHubSKILL.mdskills/code-quality/singleton/SKILL.md
GitHubSingleton
Guarantees a class has exactly one instance and provides a global access point. Use sparingly — it is essentially a global variable and makes testing harder.
When to Use
- Exactly one shared resource is needed (logger, config, connection pool, registry)
- Global state must be controlled and lazy-initialized
- Multiple instances would cause incorrect behaviour (e.g., conflicting writes)
When NOT to Use
- You just want convenient global access — prefer dependency injection instead
- The class has mutable state accessed across threads without synchronisation
How to Apply
- Make the constructor private
- Add a static field to hold the single instance
- Add a static
getInstance()method that creates the instance on first call (lazy init) - For thread safety, use double-checked locking or language-level initialisation
Example
class Database {
private static instance: Database;
private constructor(private url: string) {}
static getInstance(): Database {
if (!Database.instance) {
Database.instance = new Database(process.env.DB_URL!);
}
return Database.instance;
}
query(sql: string) { /* … */ }
}
// Usage
const db = Database.getInstance();
db.query('SELECT 1');