A Classic Example of Template Method: Sorting

An ancient example of TEMPLATE METHOD that is not only pre-Java but also prehistoric occurs in sorting.[1] Sorting has various algorithms, but in most settings, you can establish a single algorithm and use it to sort various collections of objects. Any algorithm that you use for sorting will rely on the primitive step of comparing two items, or attributes. If you can compare, say, the sharpness of two arrowheads, your sorting algorithm will let you sort the arrowheads in a collection.

[1] Some developers think of sorting as an example of STRATEGY. Challenge 23.6 on page 247 will ask you to compare STRATEGY and TEMPLATE METHOD.

In recent times, sorting probably reigns as the most frequently reimplemented ...

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.