Created
September 25, 2011 23:02
-
-
Save nitindhar7/1241293 to your computer and use it in GitHub Desktop.
Abstracting Scripts: Template Method Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class AbstractScriptTemplate { | |
// Template Method Pattern | |
public final void runScript() { | |
setup(); | |
scriptLogic(); | |
clean(); | |
report(); | |
} | |
// Methods to implement | |
public abstract void setup(); | |
public abstract void scriptLogic(); | |
public abstract void clean(); | |
// Hooks | |
public void report() { | |
System.out.println("Default report implementation"); | |
} | |
} | |
class FileMoveScript extends AbstractScriptTemplate { | |
public void setup() { | |
// do something | |
} | |
public void scriptLogic() { | |
// business logic for the task | |
} | |
public void clean() { | |
// clean up intermediate files, etc | |
} | |
} | |
class ProductPricingScript extends AbstractScriptTemplate { | |
long startTime; | |
long endTime; | |
public void setup() { | |
// do something | |
} | |
public void scriptLogic() { | |
startTime = System.nanoTime(); | |
// business logic for the task | |
} | |
public void clean() { | |
// clean up intermediate files, etc | |
} | |
@Override | |
public void report() { | |
int seconds = (startTime - endTime) / 1000000000.0 | |
System.out.println("Report finished in " + seconds + " seconds"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment