Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()

Similar presentations


Presentation on theme: "CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()"— Presentation transcript:

1 CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe() { boilWater(); boilWater(); brewCoffeeGrinds(); brewCoffeeGrinds(); pourInCup(); pourInCup(); addSugarAndMilk(); addSugarAndMilk();} public void boilWater() { System.out.println(“Boiling water”); System.out.println(“Boiling water”);} public void brewCoffeeGrinds { System.out.println(“Dripping Coffee through filter”); System.out.println(“Dripping Coffee through filter”);} public void pourInCup() { System.out.println(“Pouring in cup”); System.out.println(“Pouring in cup”);} public void addSugarAndMilk() { System.out.println(“adding Sugar and Milk”); System.out.println(“adding Sugar and Milk”);}}

2 CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Tea{ public void prepareRecipe() { public void prepareRecipe() { boilWater(); boilWater(); steepTeaBag(); steepTeaBag(); pourInCup(); pourInCup(); addLemon(); addLemon();} public void boilWater() { System.out.println(“Boiling water”); System.out.println(“Boiling water”);} public void steepTea { System.out.println(“Steeping the tea”); System.out.println(“Steeping the tea”);} public void pourInCup() { System.out.println(“Pouring in cup”); System.out.println(“Pouring in cup”);} public void addLemon() { System.out.println(“adding Lemon”); System.out.println(“adding Lemon”);}

3 CS 350 – Software Design Template Method Pattern Clearly there is code duplication. Do you think this is a contrived example? Wouldn’t you really say: public void boilWater() { System.out.println(“Boiling water for coffee”); System.out.println(“Boiling water for coffee”);} public void boilWater() { System.out.println(“Boiling water for tea”); System.out.println(“Boiling water for tea”);} As well as: public void pourInCup() { System.out.println(“Pouring coffee in cup”); System.out.println(“Pouring coffee in cup”); public void pourInCup() { System.out.println(“Pouring tea in cup”); System.out.println(“Pouring tea in cup”);

4 CS 350 – Software Design Template Method Pattern Let’s abstract Coffee and Tea:

5 CS 350 – Software Design Template Method Pattern Think of it this way: Both recipes follow the same algorithm. 1. Boil some water. 2. Use the hot water to extract the coffee or tea. 3. Pour the resulting beverage into a cup. 4. Add the appropriate condiments to the beverage.

6 CS 350 – Software Design Template Method Pattern Compare the two prepareRecipe() methods. These can be rewritten as: void prepareRecipe() { boilWater(); boilWater(); brew(); brew(); pourInCup(); pourInCup(); addCondiments(); addCondiments();} Is this better? void prepareRecipe() { boilWater(); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk(); } void prepareRecipe() { boilWater(); SteepTeaBag(); pourInCup(); addLemon(); }

7 CS 350 – Software Design Template Method Pattern If we implement the new prepare recipe, we now have: public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); boilWater(); brew(); brew(); pourInCup(); pourInCup(); addCondiments(); addCondiments();} abstract void brew(); abstract void addCondiments(); void boilWater() { System.out.println(“Boiling water”); System.out.println(“Boiling water”);} void pourInCup() { System.our.println(“Pouring in cup”); System.our.println(“Pouring in cup”);}

8 CS 350 – Software Design Template Method Pattern 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 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”);}

9 CS 350 – Software Design Template Method Pattern The Template method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps.

10 CS 350 – Software Design Template Method Pattern Template Method Up Close abstract class AbstractClass { final void templateMethod() { final void templateMethod() { primitiveOperation1(); primitiveOperation1(); primitiveOperation2(); primitiveOperation2(); concreteOpertation(); concreteOpertation();} abstract void primitiveOperation1(); abstract void primitiveOperation1(); abstract void primitiveOperation2(); abstract void primitiveOperation2(); void concreteOperation(); { void concreteOperation(); { //implementaiton here //implementaiton here }}

11 CS 350 – Software Design Template Method Pattern Template Method Up Closer abstract class AbstractClass { final void templateMethod() { final void templateMethod() { primitiveOperation1(); primitiveOperation1(); primitiveOperation2(); primitiveOperation2(); concreteOpertation(); concreteOpertation(); hook(); hook();} abstract void primitiveOperation1(); abstract void primitiveOperation1(); abstract void primitiveOperation2(); abstract void primitiveOperation2(); void concreteOperation(); { void concreteOperation(); { //implementaiton here //implementaiton here }} The hook method does nothing, but provides an interface so that subclasses may override them, although this is not required.

12 CS 350 – Software Design Template Method Pattern Using the Hook 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”);} public boolean customerWantsCondiments() { String answer = getUserInput(); String answer = getUserInput(); if ( answer.toLowerCase().startsWith(“y”)) { if ( answer.toLowerCase().startsWith(“y”)) { return true; return true; else else return false; return false; }}

13 CS 350 – Software Design Template Method Pattern Using the Hook private String GetUserInput() { String answer = null; String answer = null; System.out.print(“Would you like milk and sugar with your coffee (y/n?”); System.out.print(“Would you like milk and sugar with your coffee (y/n?”); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { try { answer = in.readLine(); answer = in.readLine(); } catch (IOException ioe) { } catch (IOException ioe) { System.err.println(“IO error trying to read your answer”); System.err.println(“IO error trying to read your answer”); } if (answer = null) { if (answer = null) { return “no”; return “no”;} return answer; }

14 CS 350 – Software Design Template Method Pattern Sorting with the Template Method public static void sort(Object[] a) { Object aux[] = (Object[])a.clone(); Object aux[] = (Object[])a.clone(); mergeSort(aux, a, 0, a.length, 0); mergeSort(aux, a, 0, a.length, 0);} private static void mergeSort (Object src[], Object dest[], int low, int high, int off) { for (int i=low; i < high; i++) { for (int i=low; i < high; i++) { for (int j=i; j>low && for (int j=i; j>low && ((Comparable)dest[j-1]).compareTo((Comparable)dest[j])>0; j--) ((Comparable)dest[j-1]).compareTo((Comparable)dest[j])>0; j--) {swap(dest, j, j-1); {swap(dest, j, j-1); } }return;;

15 CS 350 – Software Design Template Method Pattern Sorting with the Template Method 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() { return name + “weighs “ + weight; return name + “weighs “ + weight;} 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? }}}

16 CS 350 – Software Design Template Method Pattern Sorting with the Template Method 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(“Howard”, 7)}; Duck[] ducks = { new Duck(“Daffy”, 8), new Duck(“Dewey”, 2), new Duck(“Howard”, 7)}; System.outprintln(“Before sorting:”); System.outprintln(“Before sorting:”); display(ducks); display(ducks); Arrays.sort(ducks); Arrays.sort(ducks); System.out.println(“\nAfter sorting:”); System.out.println(“\nAfter sorting:”); display(ducks); display(ducks);} public static void display(duck[] ducks) { for (int i = 0; i < ducks.length; i++) { for (int i = 0; i < ducks.length; i++) { System.out.println(ducks[i]); System.out.println(ducks[i]); } }}


Download ppt "CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()"

Similar presentations


Ads by Google