Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to...

14
Template Pattern Kevin Gorski Kevin Gorski Brian Marler Brian Marler

Transcript of Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to...

Page 1: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Template Pattern

Kevin GorskiKevin Gorski

Brian MarlerBrian Marler

Page 2: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Template Definition

The intent of Template Method is to The intent of Template Method is to implement an algorithm in a method, implement an algorithm in a method, deferring the definition of some steps of the deferring the definition of some steps of the algorithm so that other classes can redefine algorithm so that other classes can redefine them.them.

Page 3: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Example: Sorting algorithm

The collections class in java.util provides a The collections class in java.util provides a sorting algorithm as a Template Method, sorting algorithm as a Template Method, deferring the comparison step to youdeferring the comparison step to you

Page 4: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Challenge 21.1

The Rocket class supplies a getApogee() The Rocket class supplies a getApogee() method that returns the height a rocket can method that returns the height a rocket can reach. This height is of type Length, a reach. This height is of type Length, a measure whose magnitude you can fetch with measure whose magnitude you can fetch with a call to getMagnitude(). Fill in the compare() a call to getMagnitude(). Fill in the compare() method in the ShowComparator program to method in the ShowComparator program to sort a collection of rockets by apogee.sort a collection of rockets by apogee.

Page 5: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Challenge 21.1

Rocket r4 = new Rocket( "Sprocket", 3.95, Rocket r4 = new Rocket( "Sprocket", 3.95, (Length) METER.times(50)); (Length) METER.times(50));

List rockets = Arrays.asList(new Rocket[] { r1, List rockets = Arrays.asList(new Rocket[] { r1, r2, r3, r4 }); r2, r3, r4 });

Comparator c = new Comparator() Comparator c = new Comparator() { {

public int compare(Object o1, Object o2)public int compare(Object o1, Object o2)

{ /* ? */ }{ /* ? */ } };};

Collections.sort(rockets, c); Collections.sort(rockets, c);

Page 6: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Challenge 21.1: SolutionComparator c = new Comparator()Comparator c = new Comparator()

{{public int compare(Object o1, Object o2)public int compare(Object o1, Object o2){{

Rocket r1 = (Rocket) o1; Rocket r1 = (Rocket) o1; Rocket r2 = (Rocket) o2; Rocket r2 = (Rocket) o2; double apogee1 = double apogee1 =

r1.getApogee().getMagnitude(); r1.getApogee().getMagnitude(); double apogee2 = double apogee2 =

r2.getApogee().getMagnitude(); r2.getApogee().getMagnitude(); return (int) (apogee1 - apogee2);return (int) (apogee1 - apogee2);

}} }; };

Page 7: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Template Method Hooks

A hook is a method call that a developer A hook is a method call that a developer places in his or her code to give other places in his or her code to give other developers a chance to insert code at a developers a chance to insert code at a specific spot in a procedure. specific spot in a procedure.

Page 8: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Our problem

After our process calls dischargePaste( ); After our process calls dischargePaste( ); and before it calls flush( ); to flush the and before it calls flush( ); to flush the processing area with water, we need to call processing area with water, we need to call another function to remove the discharged another function to remove the discharged paste. Otherwise it will be ruined by the paste. Otherwise it will be ruined by the flushing of the processing area. flushing of the processing area.

Page 9: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Original Code

public void shutDown() public void shutDown() { {

if (inProcess()) if (inProcess()) { {

stopProcessing(); stopProcessing(); markMoldIncomplete(currentMoldID); markMoldIncomplete(currentMoldID); } } usherInputMolds(); usherInputMolds(); dischargePaste(); dischargePaste(); flush(); flush();

}}

Page 10: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Solution

Add a call to collectPaste( ) after dischargePaste(), Add a call to collectPaste( ) after dischargePaste(), implementing a stub method for collectPaste implementing a stub method for collectPaste allowing developers to override it if necessary.allowing developers to override it if necessary.

Overriding dischargePaste( ) to include a Overriding dischargePaste( ) to include a collectPaste( ) call is not a desirable solution collectPaste( ) call is not a desirable solution because developers will be unaware of your call because developers will be unaware of your call and may want to modify the code in the future in and may want to modify the code in the future in some way where they don’t want to immediately some way where they don’t want to immediately collect it.collect it.

Page 11: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Refactoring to Template Method Suppose we have two different implementations of Suppose we have two different implementations of

a getPlanner( ) method as follows:a getPlanner( ) method as follows:

public MachinePlanner getPlanner() public MachinePlanner getPlanner() {{

if (planner == null) { if (planner == null) {

planner = new ShellPlanner(this); } planner = new ShellPlanner(this); }

return planner; } return planner; }

public MachinePlanner getPlanner() { public MachinePlanner getPlanner() {

if (planner == null) { if (planner == null) {

planner = new planner = new StarPressPlanner(this); } StarPressPlanner(this); } return planner; } return planner; }

Page 12: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Challenge 21.4

Suppose that you provide the Machine Suppose that you provide the Machine class with a planner attribute of type class with a planner attribute of type MachinePlanner and delete the existing MachinePlanner and delete the existing getPlanner() methods in the subclasses. getPlanner() methods in the subclasses. Write the code for getPlanner() in the Write the code for getPlanner() in the Machine classMachine class

Page 13: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Challenge 21.4: Solution

public MachinePlanner getPlanner() public MachinePlanner getPlanner() { {

if (planner == null) if (planner == null) { {

planner = createPlanner(); planner = createPlanner(); } } return planner; return planner;

} }

Page 14: Template Pattern Kevin Gorski Brian Marler. Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the.

Questions?