Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games...
-
Upload
theresa-davis -
Category
Documents
-
view
213 -
download
0
Transcript of Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games...
Template Methods
Ordering What We DoOrdering What We Do
Example - SolitaireExample - Solitaire
• Initialization of many solitaire games follow this pattern:• Shuffle the cards• Layout the game board• Deal the cards onto the table
• 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
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
• One class per game• One class that defines the overall
structure of a game and sub-classes for each particular game
Template for SolitaireTemplate for Solitaire
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Template Method DefinedTemplate 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
• 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
Template Method Structure
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Types of Methods in a Template
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
• 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
Look at Book’s Barista Example
Other ExamplesOther Examples
• Equals• Applets• Hooks for init(), start(), stop(), …
• Book’s Pizza Factory
• Equals• Applets• Hooks for init(), start(), stop(), …
• Book’s Pizza Factory