Skip to content
Back to skills

Js Template Method Pattern

ASecurity

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

  • 20 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 11, 2026
businessjavascriptgojavadocumentation

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill js-template-method-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js Template Method Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js Template Method Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-js-template-method-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-js-template-method-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# JS Template Method Pattern

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

## When to Use

- Multiple classes share the same algorithmic structure but differ in specific steps
- You want to enforce a fixed sequence of operations while allowing step customization
- Building frameworks or libraries where users extend base behavior

## 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.

```javascript
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:**

- Tight coupling through inheritance — subclasses are bound to the base class structure
- Hard to compose — you cannot mix steps from different base classes
- JavaScript's prototype chain makes "final" methods hard to enforce

**When NOT to use:**

- When steps vary independently and need to be composed freely — use Strategy pattern instead
- When there are only one or two implementations — inheritance overhead is not worth it
- When functional composition (pipe/compose) achieves the same result more flexibly

## 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

- **Type:** knowledge — this skill is a reference document, not a procedural workflow.
- **No tools or state** — consumed as context by other skills and agents.

## Success Criteria

- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.

Files in this skill

  • SKILL.md2.8 KB
  • skill.yaml714 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…