> For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt.

# Template Method

## Overview

Define the skeleton of an algorithm and let subclasses override specific steps.

## When to use

- You want consistent flow with variable steps.
- You need shared algorithm structure.

## Java example

```java
abstract class DataPipeline {
    final void run() {
        read();
        transform();
        write();
    }
    abstract void read();
    abstract void transform();
    abstract void write();
}

class CsvPipeline extends DataPipeline {
    void read() { /* csv */ }
    void transform() { /* map */ }
    void write() { /* store */ }
}
```

## TypeScript example

```ts
abstract class DataPipeline {
  run(): void {
    this.read();
    this.transform();
    this.write();
  }
  protected abstract read(): void;
  protected abstract transform(): void;
  protected abstract write(): void;
}

class JsonPipeline extends DataPipeline {
  protected read(): void {}
  protected transform(): void {}
  protected write(): void {}
}
```

## Pros and cons

Pros:

- Enforces consistent algorithm structure.
- Reduces duplicated code.

Cons:

- Inheritance can reduce flexibility.
- Hard to change the base flow.

## Common pitfalls

- Overusing inheritance for small variations.
- Adding too many hook methods.

## Related patterns

- [Strategy](https://nhanbe.id.vn/design-patterns/behavioral/strategy.md)
- [Factory Method](https://nhanbe.id.vn/design-patterns/creational/factory-method.md)
