Top Banner
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

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

Dec 30, 2015

Download

Documents

Jonah Patrick
Welcome message from author
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.
Transcript
Page 1: CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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”);}}}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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”);}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Clearly there is code duplication.Clearly there is code duplication.

Do you think this is a contrived example?Do you think this is a contrived example?

Wouldn’t you really say:Wouldn’t you really say:

public void boilWater() {public void boilWater() { System.out.println(“Boiling water for coffee”);System.out.println(“Boiling water for coffee”);}}

public void boilWater() {public void boilWater() { System.out.println(“Boiling water for tea”);System.out.println(“Boiling water for tea”);}}

As well as:As well as:

public void pourInCup() {public void pourInCup() { System.out.println(“Pouring coffee in cup”);System.out.println(“Pouring coffee in cup”);

public void pourInCup() {public void pourInCup() { System.out.println(“Pouring tea in cup”);System.out.println(“Pouring tea in cup”);

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Let’s abstract Coffee and Tea:Let’s abstract Coffee and Tea:

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Think of it this way:Think of it this way:

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.

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Compare the two prepareRecipe() methods.Compare the two prepareRecipe() methods.

These can be rewritten as:These can be rewritten as:

void prepareRecipe() {void prepareRecipe() { boilWater();boilWater(); brew();brew(); pourInCup();pourInCup(); addCondiments();addCondiments();}}

Is this better?Is this better?

void prepareRecipe() { boilWater(); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk();}

void prepareRecipe() { boilWater(); SteepTeaBag(); pourInCup(); addLemon();}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

If we implement the new prepare recipe, we now have:If we implement the new prepare recipe, we now have:

public abstract class CaffeineBeverage {public abstract class CaffeineBeverage {

final void prepareRecipe() {final void prepareRecipe() { boilWater();boilWater(); brew();brew(); pourInCup();pourInCup(); addCondiments();addCondiments();}}

abstract void brew();abstract void brew();abstract void addCondiments();abstract void addCondiments();

void boilWater() {void boilWater() { System.out.println(“Boiling water”);System.out.println(“Boiling water”);}}

void pourInCup() {void pourInCup() { System.our.println(“Pouring in cup”);System.our.println(“Pouring in cup”);}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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”);}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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.

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Template Method Up CloseTemplate Method Up Close

abstract class AbstractClass {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 }}}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Template Method Up CloserTemplate Method Up Closer

abstract class AbstractClass {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 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.

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Using the HookUsing the Hook

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”);}}

public boolean customerWantsCondiments() {public boolean customerWantsCondiments() { String answer = getUserInput();String answer = getUserInput();

if ( answer.toLowerCase().startsWith(“y”)) {if ( answer.toLowerCase().startsWith(“y”)) { return true;return true; elseelse return false;return false; }}}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Using the HookUsing the Hook

private String GetUserInput() {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;return answer;}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

Sorting with the Template MethodSorting with the Template Method

public static void sort(Object[] a) {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, private static void mergeSort (Object src[], Object dest[], int low, int high, int off)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;return;;;

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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? }}}}}

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

CS 350 – Software DesignCS 350 – Software DesignTemplate Method PatternTemplate Method Pattern

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

Duck(“Howard”, 7)};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) {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]); }} }}}}