Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games...

9
Template Methods Ordering What We Do

Transcript of Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games...

Page 1: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

Template Methods

Ordering What We DoOrdering What We Do

Page 2: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

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

Page 3: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

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

Page 4: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

Template for SolitaireTemplate for Solitaire

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 5: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

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

Page 6: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

Template Method Structure

Template Method Structure

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 7: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

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

Page 8: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

Look at Book’s Barista Example

Look at Book’s Barista Example

Page 9: Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.

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