23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

282 Part V • Handling Variations with <strong>Design</strong> <strong>Patterns</strong><br />

The Template<br />

Method is not<br />

coupled Strategies<br />

starts executing until it calls the format Connect method. Since the<br />

OracleQT object was requested to perform doQuery, the<br />

OracleOT's format Connect method is called. After this, control is<br />

returned to the Query-Template's doQuery method. The code common<br />

to all queries is now executed until the next variation is<br />

needed— the formatSelect method. Again, this method is located<br />

in the object that Query-Control is referring to (OracleQT in this<br />

example).<br />

When a new database is encountered, the Template Method provides<br />

us with a boilerplate (or template) to fill out. We create a new<br />

derived class and implement the specific steps required for the new<br />

database in it.<br />

Field Notes: Using the<br />

Template Method Pattern<br />

Sometimes a class will use several different Strategy patterns.<br />

When I first looked at the class diagram for the Template Method, I<br />

thought, "Oh, the Template Method is simply a collection of Strategies<br />

that work together." This is dangerous (and usually incorrect)<br />

thinking. While it is not uncommon for several Strategies to<br />

appear to be connected to each other, designing for this can lead to<br />

inflexibility.<br />

The Template Method is applicable when there are different, but<br />

conceptually similar processes. The variations for each process are<br />

coupled together because they are associated with a particular process.<br />

In the example I presented, when I need a format a CONNECT<br />

command for an Oracle database, if I need a format a QUERY command,<br />

it'll be for an Oracle database as well.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!