Chapter 21. Template Method

In a sense, almost every method is a template. Ordinary methods have bodies that define a sequence of instructions. It is also quite ordinary for a method to invoke methods on the current object and on other objects. Ordinary methods are, in this sense, “templates” that merely outline a series of instructions for the computer to follow. The TEMPLATE METHOD pattern, however, involves a more specific type of template.

When you write a method, you may want to define the outline of an algorithm, understanding that there may be differences in how you want to implement certain steps. In this case, you can define the method but leave some steps as abstract methods, as stubbed-out methods, or as methods defined in a separate ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.