CS 350 – Software Design CS 350 – Software Design Template Method Pattern Template Method Pattern Let’s look at two objects Let’s look at two objects public class Coffee { public class Coffee { public void prepareRecipe() { public void prepareRecipe() { boilWater(); boilWater(); brewCoffeeGrinds(); brewCoffeeGrinds(); pourInCup(); pourInCup(); addSugarAndMilk(); addSugarAndMilk(); } public void boilWater() { public void boilWater() { System.out.println(“Boiling water”); System.out.println(“Boiling water”); } public void brewCoffeeGrinds { public void brewCoffeeGrinds { System.out.println(“Dripping Coffee through filter”); System.out.println(“Dripping Coffee through filter”); } public void pourInCup() { public void pourInCup() { System.out.println(“Pouring in cup”); System.out.println(“Pouring in cup”); } public void addSugarAndMilk() { public void addSugarAndMilk() { System.out.println(“adding Sugar and Milk”); System.out.println(“adding Sugar and Milk”); } }
16
Embed
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Let’s look at two objectsLet’s look at two objects
public class Coffee {public class Coffee { public void prepareRecipe() {public void prepareRecipe() { boilWater();boilWater(); brewCoffeeGrinds();brewCoffeeGrinds(); pourInCup();pourInCup(); addSugarAndMilk();addSugarAndMilk();}}
public void boilWater() {public void boilWater() { System.out.println(“Boiling water”);System.out.println(“Boiling water”);}}public void brewCoffeeGrinds {public void brewCoffeeGrinds { System.out.println(“Dripping Coffee through filter”);System.out.println(“Dripping Coffee through filter”);}}public void pourInCup() {public void pourInCup() { System.out.println(“Pouring in cup”);System.out.println(“Pouring in cup”);}}public void addSugarAndMilk() {public void addSugarAndMilk() { System.out.println(“adding Sugar and Milk”);System.out.println(“adding Sugar and Milk”);}}}}
Let’s look at two objectsLet’s look at two objects
public class Tea{public class Tea{ public void prepareRecipe() {public void prepareRecipe() { boilWater();boilWater(); steepTeaBag();steepTeaBag(); pourInCup();pourInCup(); addLemon();addLemon();}}
public void boilWater() {public void boilWater() { System.out.println(“Boiling water”);System.out.println(“Boiling water”);}}
public void steepTea {public void steepTea { System.out.println(“Steeping the tea”);System.out.println(“Steeping the tea”);}}
public void pourInCup() {public void pourInCup() { System.out.println(“Pouring in cup”);System.out.println(“Pouring in cup”);}}
public void addLemon() {public void addLemon() { System.out.println(“adding Lemon”);System.out.println(“adding Lemon”);}}
Both recipes follow the same algorithm.Both recipes follow the same algorithm.
1.1. Boil some water.Boil some water.2.2. Use the hot water to extract the coffee or tea.Use the hot water to extract the coffee or tea.3.3. Pour the resulting beverage into a cup.Pour the resulting beverage into a cup.4.4. Add the appropriate condiments to the beverage.Add the appropriate condiments to the beverage.
public class Tea extends CaffeineBeverage {public class Tea extends CaffeineBeverage { public void brew() {public void brew() { System.out.println(“Steeping the tea”);System.out.println(“Steeping the tea”);}} public void addCondiments() {public void addCondiments() { System.out.println(“Adding Lemon”);System.out.println(“Adding Lemon”);}}
public class Coffee extends CaffeineBeverage {public class Coffee extends CaffeineBeverage { public void brew() {public void brew() { System.out.println(“Dripping Coffee through filter”);System.out.println(“Dripping Coffee through filter”);}} public void addCondiments() {public void addCondiments() { System.out.println(“Adding Sugar and Milk”);System.out.println(“Adding Sugar and Milk”);}}
The Template method defines the steps of an algorithm and allows The Template method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps.subclasses to provide the implementation for one or more steps.
The hook method does nothing, but provides an interface so that The hook method does nothing, but provides an interface so that subclasses may override them, although this is not required.subclasses may override them, although this is not required.
public class CoffeeWithHook extends CaffeineBeverageWithHook {public class CoffeeWithHook extends CaffeineBeverageWithHook { public void brew() {public void brew() { System.out.println(“Dripping Coffee through filter”);System.out.println(“Dripping Coffee through filter”);}} public void addCondiments() {public void addCondiments() { System.out.println(“Adding Sugar and Milk”);System.out.println(“Adding Sugar and Milk”);}}
Sorting with the Template MethodSorting with the Template Method
public class Duck implements Comparable {public class Duck implements Comparable { String name;String name; int weight; int weight;
public Duck(String name, int weight) {public Duck(String name, int weight) { this.name = name;this.name = name; this.weight = weight;this.weight = weight;}}
public String toString() {public String toString() { return name + “weighs “ + weight;return name + “weighs “ + weight;}}
public int compareTo(Object object) {public int compareTo(Object object) { Duck otherDuck = (Duck)object;Duck otherDuck = (Duck)object;
if (this.weight < otherDuck.weight) {if (this.weight < otherDuck.weight) { return -1;return -1; } else if (this.weight > otherDuck.weight) {} else if (this.weight > otherDuck.weight) { return 1;return 1; } else { return She is a witch, may we burn her? }} else { return She is a witch, may we burn her? }}}}}
Sorting with the Template MethodSorting with the Template Method
public class DuckSortTestDrive {public class DuckSortTestDrive { public static void main (String[] args) {public static void main (String[] args) { Duck[] ducks = { new Duck(“Daffy”, 8), new Duck(“Dewey”, 2), new Duck[] ducks = { new Duck(“Daffy”, 8), new Duck(“Dewey”, 2), new