From harness-claude
Implements the Singleton pattern in JavaScript — ensures a class has only one instance and provides a global access point. Covers class-based and ESM module-level singletons, with guidance on testing and trade-offs.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:js-singleton-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Ensure a class has only one instance and provide a global access point
Ensure a class has only one instance and provide a global access point
getInstance() method that returns the existing instance or creates one on first call.getInstance(), not the class itself.// Preferred ESM approach — module-level singleton
let instance;
class DatabaseConnection {
constructor(url) {
if (instance) return instance;
this.url = url;
this.connected = false;
instance = this;
}
connect() {
this.connected = true;
}
}
export const getInstance = (url) => new DatabaseConnection(url);
Object.freeze(instance) if it should be immutable after creation.The Singleton pattern is one of the most debated patterns in JavaScript. The classic implementation uses a class with a static instance, but ES modules provide a simpler alternative: any module-level variable is effectively a singleton because Node.js and browsers cache module exports after the first import.
Trade-offs:
When NOT to use:
Related patterns:
https://patterns.dev/javascript/singleton-pattern
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeImplements singleton patterns in TypeScript via module-level singletons, class-based patterns, and WeakRef. Useful for shared database pools, loggers, and configs.
Ensures a class has exactly one instance with a global access point. Useful for configuration stores, connection pools, and loggers where multiple instances would cause resource conflicts.
Covers 26 Gang of Four design patterns with PHP 8.3+ implementations, UML diagrams, and practical use cases.