Computer Science 313 – Advanced Programming Topics
Template Method Intent Enable specialization of general algorithm Superclass defines algorithm skeleton & basics Skeleton filled out and detailed by subclasses Client uses algorithm without needing specifics Makes coding simple as possible General algorithm coded in the superclass Defines simple method(s) uncluttered by detail Focus on the details left to subclasses Subclass methods simplified by monofocus
In the Beginning…
Template Method Example Consider creating a log of program actions: Store in an HTML file for display on web Create easy searching by writing simple text file results for immediate action
Logger Algorithm Input: String of text to log 1. Create new file in which results stored 2. If file needs a header to be correct 1. Print out the file header 3. Print out the text to the file 4. If file trailer is needed for correctness 1. Print out the file trailer 5. Perform actions completing the logging
Create the Superclass
Logger Algorithm Input: String of text to log 1. Create new file in which results stored 2. If file needs a header to be correct 1. Print out the file header 3. Print out the text to the file 4. If file trailer is needed for correctness 1. Print out the file trailer 5. Perform actions completing the logging
Logger Superclass final public abstract class Logger { public final void logIt(String text){ File log = createLogFile(); if (needsHeader()) { printHeader(log); } logText(log, text); if (needsTrailer()) { printTrailer(log); } completeLog(log); }
Should Method be Hook? If method’s steps known & cannot be changed Subclasses do not need to define it at all Can define this as private method in superclass Include code directly and do not make abstract If the action is optional or usually works 1 way Define simple/empty method in superclass DO NOT MAKE FINAL DO NOT MAKE FINAL ; subclasses may override If you have no clue how method works Leave for subclasses; make method abstract
Hooks in Logger public abstract class Logger { public final void logIt(String text){ File log = createLogFile(); if (needsHeader()) { printHeader(log); } logText(log, text); if (needsTrailer()) { printTrailer(log); } completeLog(log); }
Bring Out the Hook public abstract class Logger { public boolean needsHeader() { return false; } public boolean needsTrailer() { return false; } public void printHeader(File log){ } public void printTrailer(File log){ } }
Define the Hook public class HTMLLogger extends Logger { public boolean needsHeader() { return true; } public void printHeader(File log){ PrintWriter w = new PrintWriter(log); w.println(“ ”); } // Similar code for trailer methods
Think of the Children! public abstract class Logger { public final void logIt(String text){ File log = createLogFile(); if (needsHeader()) { printHeader(log); } logText(log, text); if (needsTrailer()) { printTrailer(log); } completeLog(log); }
Today’s Activity
Template Method Pattern public abstract class Activity { public final void makeEmWork() { doSomething(); discussProblem(); doSomethingElse(); Mug m = getMug(); doSomethingMore(m); solveProblem(m); concludeSomething(); }
For Next Class Lab due next Friday, so get cracking When we return discuss tree-based design pattern Have a great Easter break!