8 The Template Method and Strategy Design Patterns
This chapter covers
- The Template Method Design Pattern
- The Strategy Design Pattern
Each design pattern is an industry-proven model from which we can create a welldesigned solution to a common software architecture problem. A design pattern is only a model because it is rarely a solution we can use directly—we use it as the basis to create a custom solution to an architecture problem in our application. This chapter covers two design patterns.
Application development, and programming in general, involves algorithms and data. The data are the ingredients, and the algorithms are the steps of the recipe. The two design patterns in this chapter, the Template Method Design Pattern and the Strategy Design Pattern, are models for a software architecture that must manage multiple algorithms.
Our example application for the Template Method Design Pattern generates sports reports. The basic outline of the reports is the same, but parts of the report differ. The example application for the Strategy Design Pattern shows how to deploy strategies to recruit players and reserve venues for different sports. The strategies are interchangeable, and at run time, the application can determine which one to deploy, depending on the sport.
For each of the two patterns, we’ll have example code that doesn’t use the pattern, followed by example code modeled by the pattern. Then, we’ll see the improvement the pattern makes.