160 likes | 249 Views
Computer Science 313 – Advanced Programming Topics. Lecture 30: Template method Implementation. Template Method Intent. Enable specialization of general algorithm Superclass defines algorithm skeleton & basics Skeleton filled out and detailed by subclasses
E N D
Computer Science 313 – Advanced Programming Topics Lecture 30:Template method Implementation
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… • For this pattern, it starts with the algorithm • Single algorithm required to use this pattern • Algorithm can then be executed in multiple ways • Outline steps of algorithm in superclass • Need to keep algorithm as general as possible • Specify optional portions very, very clearly • Ensure you identify anything that may change • When in doubt, leave it out
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 • E-mail results for immediate action
Logger Algorithm Input: String of text to log • Create new file in which results stored • If file needs a header to be correct • Print out the file header • Print out the text to the file • If file trailer is needed for correctness • Print out the file trailer • Perform actions completing the logging
Create the Superclass • Define the pattern’s abstractsuperclass • Define fields & constructors for the class • Superclass that is not abstract used in other pattern • Write method defining algorithm in superclass • For each step, define methods to perform actions • Make method final since it is complete • Superclass only focuses on general algorithm • Template Method needs algorithm as general step
Logger Algorithm Input: String of text to log • Create new file in which results stored • If file needs a header to be correct • Print out the file header • Print out the text to the file • If file trailer is needed for correctness • Print out the file trailer • Perform actions completing the logging
Logger Superclass public abstract class Logger {public finalvoid 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;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 booleanneedsHeader() { return false;}public booleanneedsTrailer() { return false;}public void printHeader(File log){ }public void printTrailer(File log){ } }
Define the Hook public class HTMLLogger extends Logger { public booleanneedsHeader() { return true;}public void printHeader(File log){PrintWriter w = new PrintWriter(log);w.println(“<html><body>”);}// 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 • Work in 3s to write at least 1-page scene • All people will act out scene at lecture’s end • Your scene must begin in a meeting/lecture • Must include mug at some point during scene • Scene should discuss real problem people have • Template method pattern must solve problem
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!