1 / 15

Lecture 30: Template method Implementation

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

pules
Download Presentation

Lecture 30: Template method Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 30:Template method Implementation

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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);}

  9. 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

  10. 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);}

  11. 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){ } }

  12. 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

  13. 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);}

  14. Today’s Activity • Work in pairs to write out a scene • All team members act our scene at lecture’s end • All actors will need to hold cup during scene • Template method pattern must solve problem

  15. For Next Class • Lab due in about 1 hour • Reports due Monday after break • Will be useful for Test#2 week after we get back • Have a fun Spring Break!

More Related