Skip to content

Instantly share code, notes, and snippets.

@nitindhar7
Created September 25, 2011 23:02
Show Gist options
  • Save nitindhar7/1241293 to your computer and use it in GitHub Desktop.
Save nitindhar7/1241293 to your computer and use it in GitHub Desktop.
Abstracting Scripts: Template Method Pattern
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