From harness-claude
Implements singleton patterns in TypeScript via module-level singletons, class-based patterns, and WeakRef. Useful for shared database pools, loggers, and configs.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:gof-singletonThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Ensure a class has exactly one instance using module-level singletons and WeakRef patterns.
Ensure a class has exactly one instance using module-level singletons and WeakRef patterns.
new Service() calls across the codebase that all should share one instancePrefer module-level singletons over class-based singletons in TypeScript. Node.js module caching gives you singleton behavior for free.
Module-level singleton (preferred):
// db.ts — Node's module cache guarantees one instance
import { Pool } from 'pg';
let pool: Pool | null = null;
export function getPool(): Pool {
if (!pool) {
pool = new Pool({ connectionString: process.env.DATABASE_URL });
}
return pool;
}
export async function closePool(): Promise<void> {
if (pool) {
await pool.end();
pool = null;
}
}
Class-based singleton (when interface polymorphism matters):
class Logger {
private static instance: Logger | null = null;
private readonly prefix: string;
private constructor(prefix: string) {
this.prefix = prefix;
}
static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger('[App]');
}
return Logger.instance;
}
log(message: string): void {
console.log(`${this.prefix} ${new Date().toISOString()} ${message}`);
}
}
// Usage
const logger = Logger.getInstance();
logger.log('Server started');
Reset for testing — always expose a reset method or use a factory:
class ConfigStore {
private static instance: ConfigStore | null = null;
static getInstance(): ConfigStore {
if (!ConfigStore.instance) {
ConfigStore.instance = new ConfigStore();
}
return ConfigStore.instance;
}
// Only for tests — do not expose in production API
static _reset(): void {
ConfigStore.instance = null;
}
}
WeakRef variant (allows GC when no other references exist):
class ExpensiveCache {
private static ref: WeakRef<ExpensiveCache> | null = null;
static getInstance(): ExpensiveCache {
const existing = ExpensiveCache.ref?.deref();
if (existing) return existing;
const instance = new ExpensiveCache();
ExpensiveCache.ref = new WeakRef(instance);
return instance;
}
}
Why module-level beats class-based: TypeScript compiles to CommonJS or ESM. In CommonJS, require() caches modules — the first require('./db') runs the module, subsequent calls return the cached export. This is a singleton. In ESM, the module instance is also cached per URL. Class-based singletons add ceremony without benefit in most cases.
Anti-patterns:
When to use dependency injection instead:
// Prefer this for services that need to be tested or swapped
class OrderService {
constructor(private readonly db: DatabasePool) {}
}
// Wire at app start
const db = getPool();
const orderService = new OrderService(db);
Singleton vs. service locator: A singleton is discoverable from anywhere; a service locator is explicit injection. Singletons are fine for infrastructure (logger, config, pool) but anti-patterns for business logic.
Lazy initialization matters for startup cost: Do not initialize singletons at import time if they open connections — use lazy getInstance() so tests don't need live infrastructure to import a module.
refactoring.guru/design-patterns/singleton
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeImplements 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.
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.