OnJava8-Examples/patterns/TemplateMethod.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

43 lines
839 B
Java

//: patterns/TemplateMethod.java
// ©2015 MindView LLC: see Copyright.txt
// Simple demonstration of Template Method.
abstract class ApplicationFramework {
public ApplicationFramework() {
templateMethod();
}
abstract void customize1();
abstract void customize2();
// "private" means automatically "final":
private void templateMethod() {
for(int i = 0; i < 5; i++) {
customize1();
customize2();
}
}
}
// Create a new "application":
class MyApp extends ApplicationFramework {
@Override
void customize1() {
System.out.print("Hello ");
}
@Override
void customize2() {
System.out.println("World!");
}
}
public class TemplateMethod {
public static void main(String args[]) {
new MyApp();
}
} /* Output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
*///:~