Js template method pattern

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

Js template method pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Define the skeleton of an algorithm in a base class and let subclasses override specific steps

How to use this skill

Skill file

JS Template Method Pattern

Define the skeleton of an algorithm in a base class and let subclasses override specific steps

When to Use

Instructions

  1. Create a base class with a "template method" that calls a series of step methods in order.
  2. Implement default behavior for shared steps; leave varying steps abstract or as no-ops.
  3. Subclasses override only the steps they need to customize.
  4. Never let subclasses change the template method itself — keep it final via convention or documentation.
class DataProcessor {
  process(data) {
    const validated = this.validate(data);
    const transformed = this.transform(validated);
    return this.format(transformed);
  }
  validate(data) {
    return data;
  } // default: no-op
  transform(data) {
    throw new Error('Subclass must implement transform()');
  }
  format(data) {
    return JSON.stringify(data);
  } // default: JSON
}

class CSVProcessor extends DataProcessor {
  transform(data) {
    return data.map((row) => row.join(','));
  }
  format(data) {
    return data.join('\n');
  }
}

Details

The Template Method pattern uses inheritance to vary parts of an algorithm. The base class defines the algorithm's skeleton, and subclasses fill in the blanks. This is the inverse of the Strategy pattern — Template Method uses inheritance, Strategy uses composition.

Trade-offs:

When NOT to use:

Source

https://patterns.dev/javascript/template-method-pattern

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.