# Template Method

Template method defines the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Base class declares algorithm 'placeholders', and derived classes implement the placeholders.

## Example - Building a house

We are going to make "algorithm" to build a house. First we want to build foundation, then walls, then windows and roof as the last thing.

```
abstract class HouseTemplate {

    public void build() {
        buildFoundation();
        buildWalls();
        buildWindows();
        buildRoof();
    }

    protected abstract void buildFoundation();
    protected abstract void buildWalls();
    protected abstract void buildWindows();
    protected abstract void buildRoof();
}

class WoodenHouse extends HouseTemplate {

    @Override
    protected void buildFoundation() {
        System.out.println("Foundation");
    }

    @Override
    protected void buildWalls() {
        System.out.println("Wooden Walls");
    }

    @Override
    protected void buildWindows() {
        System.out.println("Windows");
    }

    @Override
    protected void buildRoof() {
        System.out.println("Wooden Roof");
    }
}
```

Then we build a wooden house like this.

```
WoodenHouse house = new WoodenHouse();
house.build();
```

## Default implementations

We might find that some steps are the same in all implementations of template class. That might be case to put these implementations up, to the template class. We might move `buildFoundation` and `buildRoof` into template class because those could be implemented in the same way for all house types. And if not, then we can always override `buildFoundation` and `buildRoof` methods.

```
abstract class HouseTemplate {

    public void build() {
        buildFoundation();
        buildWalls();
        buildWindows();
        buildRoof();
    }

    protected abstract void buildFoundation() {
        System.out.println("Foundation");
    }

    protected abstract void buildWalls();

    protected void buildWindows() {
        System.out.println("Windows");
    }

    protected abstract void buildRoof();
}

class WoodenHouse extends HouseTemplate {

    @Override
    protected void buildWalls() {
        System.out.println("Wooden Walls");
    }

    @Override
    protected void buildRoof() {
        System.out.println("Wooden Roof");
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/design-patterns-handbook/behavioral-patterns/template-method.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
