Download presentation
Presentation is loading. Please wait.
Published byKelly Williamson Modified over 9 years ago
1
Template Pattern Kevin Gorski Brian Marler
2
Template Definition The intent of Template Method is to implement an algorithm in a method, deferring the definition of some steps of the algorithm so that other classes can redefine them. The intent of Template Method is to implement an algorithm in a method, deferring the definition of some steps of the algorithm so that other classes can redefine them.
3
Example: Sorting algorithm The collections class in java.util provides a sorting algorithm as a Template Method, deferring the comparison step to you The collections class in java.util provides a sorting algorithm as a Template Method, deferring the comparison step to you
4
Challenge 21.1 The Rocket class supplies a getApogee() method that returns the height a rocket can reach. This height is of type Length, a measure whose magnitude you can fetch with a call to getMagnitude(). Fill in the compare() method in the ShowComparator program to sort a collection of rockets by apogee. The Rocket class supplies a getApogee() method that returns the height a rocket can reach. This height is of type Length, a measure whose magnitude you can fetch with a call to getMagnitude(). Fill in the compare() method in the ShowComparator program to sort a collection of rockets by apogee.
5
Challenge 21.1 Rocket r4 = new Rocket( "Sprocket", 3.95, (Length) METER.times(50)); List rockets = Arrays.asList(new Rocket[] { r1, r2, r3, r4 }); Comparator c = new Comparator() { public int compare(Object o1, Object o2) { /* ? */ } { /* ? */ } }; }; Collections.sort(rockets, c); Collections.sort(rockets, c);
6
Challenge 21.1: Solution Comparator c = new Comparator() { public int compare(Object o1, Object o2) { Rocket r1 = (Rocket) o1; Rocket r2 = (Rocket) o2; double apogee1 = r1.getApogee().getMagnitude(); double apogee2 = r2.getApogee().getMagnitude(); return (int) (apogee1 - apogee2); } }; };
7
Template Method Hooks A hook is a method call that a developer places in his or her code to give other developers a chance to insert code at a specific spot in a procedure. A hook is a method call that a developer places in his or her code to give other developers a chance to insert code at a specific spot in a procedure.
8
Our problem After our process calls dischargePaste( ); and before it calls flush( ); to flush the processing area with water, we need to call another function to remove the discharged paste. Otherwise it will be ruined by the flushing of the processing area. After our process calls dischargePaste( ); and before it calls flush( ); to flush the processing area with water, we need to call another function to remove the discharged paste. Otherwise it will be ruined by the flushing of the processing area.
9
Original Code public void shutDown() { if (inProcess()) { stopProcessing(); markMoldIncomplete(currentMoldID); }usherInputMolds();dischargePaste();flush();}
10
Solution Add a call to collectPaste( ) after dischargePaste(), implementing a stub method for collectPaste allowing developers to override it if necessary. Add a call to collectPaste( ) after dischargePaste(), implementing a stub method for collectPaste allowing developers to override it if necessary. Overriding dischargePaste( ) to include a collectPaste( ) call is not a desirable solution because developers will be unaware of your call and may want to modify the code in the future in some way where they don’t want to immediately collect it. Overriding dischargePaste( ) to include a collectPaste( ) call is not a desirable solution because developers will be unaware of your call and may want to modify the code in the future in some way where they don’t want to immediately collect it.
11
Refactoring to Template Method Suppose we have two different implementations of a getPlanner( ) method as follows: Suppose we have two different implementations of a getPlanner( ) method as follows: public MachinePlanner getPlanner() { if (planner == null) { planner = new ShellPlanner(this); } return planner; } public MachinePlanner getPlanner() { if (planner == null) { planner = new StarPressPlanner(this); } return planner; }
12
Challenge 21.4 Suppose that you provide the Machine class with a planner attribute of type MachinePlanner and delete the existing getPlanner() methods in the subclasses. Write the code for getPlanner() in the Machine class Suppose that you provide the Machine class with a planner attribute of type MachinePlanner and delete the existing getPlanner() methods in the subclasses. Write the code for getPlanner() in the Machine class
13
Challenge 21.4: Solution public MachinePlanner getPlanner() { if (planner == null) { planner = createPlanner(); } return planner; } public MachinePlanner getPlanner() { if (planner == null) { planner = createPlanner(); } return planner; }
14
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.