From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Understanding the Template Method pattern

Understanding the Template Method pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Understanding the Template Method pattern

- [Bethan] The template method pattern is all about defining the common steps in an algorithm and a superclass and redefining some of those steps in a subclass. This is actually one of the most commonly used design patterns. As a real life example, imagine there's a recipe for making lasagna. The recipe defines a set of steps which you follow to cook the lasagna, but there might be alternative versions with different fillings. For example, there might be a beef mince lasagna recipe, a vegetable lasagna recipe, or a lentil lasagna recipe, and they might also have different methods for making things like the white sauce. Some of the steps might be the same for every lasagna, for example, constructing the lasagna and cooking it in the oven. In code, you would define the steps for making a lasagna in an abstract class, for example, with methods for making the filling, making the white sauce, and baking the lasagna. The…

Contents