From harness-claude
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.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Ensure a class has exactly one instance using module-level singletons and WeakRef patterns.
> Ensure a class has only one instance and provide a global access point
Applies design patterns (Singleton, Factory, Observer, Strategy, etc.) to refactor code architecture, implement extensible systems, and follow SOLID principles.
Provides PHP 8.3+ implementations, UML guidance, real-world examples, trade-offs, and anti-patterns for 26 Gang of Four design patterns. Activates for applying patterns, refactoring code, selecting patterns, or reviewing usage.
Share bugs, ideas, or general feedback.
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