Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 313 – Advanced Programming Topics.

Similar presentations


Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

1 Computer Science 313 – Advanced Programming Topics

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…

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  results for immediate action

5 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

6 Create the Superclass

7 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

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

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  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 boolean needsHeader() { return false; } public boolean needsTrailer() { return false; } public void printHeader(File log){ } public void printTrailer(File log){ } }

12 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

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

15 Template Method Pattern public abstract class Activity { public final void makeEmWork() { doSomething(); discussProblem(); doSomethingElse(); Mug m = getMug(); doSomethingMore(m); solveProblem(m); concludeSomething(); }

16 For Next Class  Lab due next Friday, so get cracking  When we return discuss tree-based design pattern  Have a great Easter break!


Download ppt "Computer Science 313 – Advanced Programming Topics."

Similar presentations


Ads by Google