Template Methods

Template Methods
Ordering What We Do
Example - Solitaire
• Initialization of many solitaire
games follow this pattern:
• Shuffle the cards
• Layout the game board
• Deal the cards onto the table
Options for building
two solitaire games
• One class per game
• One class that defines the
overall structure of a game
and sub-classes for each
particular game
Template for Solitaire
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
Template Method
Defined
• The Template Method Pattern
defines the skeleton of an
algorithm in a method,
deferring some steps to
subclasses. It lest subclasses
redefine certain steps of an
algorithm without changing the
algorithm’s structure
Template Method
Structure
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
Types of Methods in a
Template
• Primitive Operations
• Abstract and left to subclasses for
definition
• Concrete Operations
• Common for all subclasses, so
concrete in the template class
• Hooks
• Default implementation in the Template
class, but can be overridden by
subclasses
• Usually do nothing if not overridden
Look at Book’s Barista
Example
Other Examples
• Equals
• Applets
• Hooks for init(), start(), stop(), …
• Book’s Pizza Factory