From harness-claude
> Ensure a class has only one instance and provide a global access point
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Ensure a class has only one instance and provide a global access point
Implements Singleton pattern in TypeScript/Node.js using module-level singletons (preferred via module cache) and WeakRef for shared resources like DB pools, loggers, configs. Use to avoid repeated object creation.
Applies design patterns (Singleton, Factory, Observer, Strategy, etc.) to refactor code architecture, implement extensible systems, and follow SOLID principles.
Mandates invoking relevant skills via tools before any response in coding sessions. Covers access, priorities, and adaptations for Claude Code, Copilot CLI, Gemini CLI.
Share bugs, ideas, or general feedback.
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