Applying the Template Method to the Case Study

In this case study, the variations in database access occur in the particular implementations of the steps involved. Figure 18-1 illustrates this.

Figure 18-1. Using the Template Method pattern to perform a query.

I have created a method called doQuery that handles the query I need to perform. I pass in the name of the database and the query specification. The doQuery method follows the five general steps above, providing virtual methods for the steps (such as formatConnect and formatSelect) that must be implemented differently.

The doQuery method is implemented as follows. As shown in Figure 18-1 ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design 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.