23-templatemethoditeratorco

23-templatemethoditeratorco - Template Method, Iterator...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 23 — 11/10/2008 © University of Colorado, 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Goals • Cover Material from Chapter 8 and 9 of the Design Patterns Textbook • Template Method Pattern • Iterator Pattern • Composite Pattern 2
Background image of page 2
Template Method: Defnition • The Template Method Pattern defnes the skeleton oF an algorithm in a method, deFerring some steps to subclasses. Template Method lets subclasses redefne certain steps oF an algorithm without changing the algorithm’s structure • Template Method defnes the steps oF an algorithm and allows subclasses to provide the implementation For one or more steps • Makes the algorithm abstract • Each step oF the algorithm is represented by a method • Encapsulates the details oF most steps • Steps (methods) handled by subclasses are declared abstract • Shared steps (concrete methods) are placed in the same class that has the template method, allowing For code re-use among the various subclasses 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Template Method: Structure templateMethod() primitiveOperation1() primitiveOperation2() AbstractClass primitiveOperation1() primitiveOperation2() ConcreteClass primitiveOperation1(); primitiveOperation2() Very simple pattern… ...but also very powerful Used typically in application frameworks, e.g. Cocoa and .Net primitiveOperation1() and primitiveOperation2() are sometimes referred to as hook methods as they allow subclasses to hook their behavior into the service provided by AbstractClass 4
Background image of page 4
Example: Tea and Coffee • The book returns to the Starbuzz example and shows the training guide for baristas and, in particular, the recipes for making coffee and tea • Coffee • Boil water • Brew coffee in boiling water • Pour coffee in cup • Add sugar and milk • Tea • Boil water • Steep tea in boiling water • Pour tea in cup • Add lemon 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Coffee Implementation public class Coffee { 1 2 void prepareRecipe() { 3 boilWater(); 4 brewCoffeeGrinds(); 5 pourInCup(); 6 addSugarAndMilk(); 7 } 8 9 public void boilWater() { 10 System.out.println( "Boiling water" ); 11 } 12 13 public void brewCoffeeGrinds() { 14 System.out.println( "Dripping Coffee through filter" ); 15 } 16 17 public void pourInCup() { 18 System.out.println( "Pouring into cup" ); 19 } 20 21 public void addSugarAndMilk() { 22 System.out.println( "Adding Sugar and Milk" ); 23 } 24 } 25 26 6
Background image of page 6
Tea Implementation public class Tea { 1 2 void prepareRecipe() { 3 boilWater(); 4 steepTeaBag(); 5 pourInCup(); 6 addLemon(); 7 } 8 9 public void boilWater() { 10 System.out.println( "Boiling water" ); 11 } 12 13 public void steepTeaBag() { 14 System.out.println( "Steeping the tea" ); 15 } 16 17 public void addLemon() { 18 System.out.println( "Adding Lemon" ); 19 } 20 21 public void pourInCup() { 22 System.out.println( "Pouring into cup" ); 23 } 24 } 25 26 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Code Duplication! • We have code duplication occurring in these two classes
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

23-templatemethoditeratorco - Template Method, Iterator...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online