DOC PREVIEW
CU-Boulder CSCI 6448 - Template Method

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Template MethodKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 24 — 11/15/2007© University of Colorado, 20071Friday, November 16, 2007Lecture Goals• Cover Material from Chapter 8 of the Design Patterns Textbook• Template Method Pattern2Friday, November 16, 2007Template 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 subclasses3Friday, November 16, 2007Template 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 AbstractClass4Friday, November 16, 2007Example: 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 lemon5Friday, November 16, 2007Coffee 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}25266Friday, November 16, 2007Tea 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}25267Friday, November 16, 2007Code 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()Tea8Friday, November 16, 2007Similar 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 it9Friday, November 16, 2007CaffeineBeverage 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 subclasses10Friday, November 16, 2007Coffee 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!11Friday, November 16, 2007What 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 them12Friday, November 16, 2007Comparison: 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 pieces13Friday, November 16, 2007The 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


View Full Document

CU-Boulder CSCI 6448 - Template Method

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Template Method
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 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 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?