CU-Boulder CSCI 5448 - Template Method, Iterator & Composite

Unformatted text preview:

Template Method, Iterator & CompositeKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/5448 — Lecture 23 — 11/10/2008© University of Colorado, 20091Lecture Goals• Cover Material from Chapter 8 and 9 of the Design Patterns Textbook• Template Method Pattern• Iterator Pattern• Composite Pattern2Template Method: Definition• The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure• Template Method defines 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 subclasses3Template Method: StructuretemplateMethod()primitiveOperation1()primitiveOperation2()AbstractClassprimitiveOperation1()primitiveOperation2()ConcreteClassprimitiveOperation1();primitiveOperation2()Very simple pattern…...but also very powerfulUsed typically in application frameworks, e.g. Cocoa and .NetprimitiveOperation1() and primitiveOperation2() are sometimes referred to as hook methods as they allow subclasses to hook their behavior into the service provided by AbstractClass4Example: 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 lemon5Coffee Implementationpublic 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}25266Tea Implementationpublic 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}25267Code Duplication!• We have code duplication occurring in these two classes• boilWater() and pourInCup() are exactly the same• Lets get rid of the duplicationprepareRecipe()boilWater()pourInCup()CaffeineBeverageprepareRecipe()brewCoffeeGrinds()addSugarAndMilk()CoffeeprepareRecipe()steepTea()addLemon()Tea8Similar algorithms• The structure of the algorithms in prepareRecipe() is similar for Tea and Coffee• We can improve our code further by making the code in prepareRecipe() more abstract• brewCoffeeGrinds() and steepTea() 󲰛 brew()• addSugarAndMilk() and addLemon() 󲰛 addCondiments()• Excellent, now all we need to do is specify this structure in CaffeineBeverage.prepareRecipe() and make it such that subclasses can’t change the structure• How do we do that?• Answer: By convention OR by using the keyword “final” in languages that support it9CaffeineBeverage Implementationpublic abstract class CaffeineBeverage {1 2 final void prepareRecipe() {3 boilWater();4 brew();5 pourInCup();6 addCondiments();7 }8 9 abstract void brew();10 11 abstract void addCondiments();12 13 void boilWater() {14 System.out.println("Boiling water");15 }16 17 void pourInCup() {18 System.out.println("Pouring into cup");19 }20}2122Note: use of final keyword for prepareReceipe()brew() and addCondiments() are abstract and must be supplied by subclassesboilWater() and pourInCup() are specified and shared across all subclasses10Coffee And Tea Implementationspublic class Coffee extends CaffeineBeverage {1 public void brew() {2 System.out.println("Dripping Coffee through filter");3 }4 public void addCondiments() {5 System.out.println("Adding Sugar and Milk");6 }7}89public class Tea extends CaffeineBeverage {10 public void brew() {11 System.out.println("Steeping the tea");12 }13 public void addCondiments() {14 System.out.println("Adding Lemon");15 }16}1718Nice and Simple!11What have we done?• Took two separate classes with separate but similar algorithms• Noticed duplication and eliminated it by introducing a superclass• Made steps of algorithm more abstract and specified its structure in the superclass• Thereby eliminating another “implicit” duplication between the two classes• Revised subclasses to implement the abstract (unspecified) portions of the algorithm… in a way that made sense for them12Comparison: Template Method (TM) vs. No TM•No Template Method• Coffee and Tea each have own copy of algorithm• Code is duplicated across both classes• A change in the algorithm would result in a change in both classes• Not easy to add new caffeine beverage• Knowledge of algorithm distributed over multiple classes•Template Method• CaffeineBeverage has the algorithm and protects it• CaffeineBeverage shares common code with all subclasses• A change in the algorithm likely impacts only CaffeineBeverage• New caffeine beverages can easily be plugged in• CaffeineBeverage centralizes knowledge of the algorithm; subclasses plug in missing pieces13The Book’s Hook• Previously I called the abstract methods that appear in a template method “hook” methods• The book refers to hook methods as well, but they make the following distinction: a hook method is a concrete method that appears in the AbstractClass that has an empty method body (or a mostly empty method body, see example next slide), i.e.• public void hook() {}• Subclasses are free to override them but don’t have to since they


View Full Document

CU-Boulder CSCI 5448 - Template Method, Iterator & Composite

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Template Method, Iterator & Composite
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Template Method, Iterator & Composite and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Template Method, Iterator & Composite 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?