Gof singleton

From Intense-Visions/harness-engineering · View interactive version

Gof singleton is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Ensure a class has exactly one instance using module-level singletons and WeakRef patterns.

How to use this skill

Skill file

GOF Singleton

Ensure a class has exactly one instance using module-level singletons and WeakRef patterns.

When to Use

Instructions

Prefer 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;
  }
}

Details

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.

Source

refactoring.guru/design-patterns/singleton

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.