Part 1 of 6 · 1 min read
Why this page comes seventh
In this module you will
- State the Template Method pattern and name its parts.
- Write an abstract class with a final template method, abstract steps and a hook.
- Explain the Hollywood principle: the parent calls the subclass, never the reverse.
- Avoid the constructor trap that Effective Java warns about.
- Choose between Template Method and Strategy.
Strategy swaps a whole algorithm by composition. Template Method fixes the order of an algorithm and lets subclasses change single steps, using inheritance. It appears in frameworks and in code that has several similar classes with the same outline. The sources are "Design Patterns" (GoF) chapter 5, "Head First Design Patterns" (HF) chapter 8, and "Effective Java" (EJ) Item 19. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.