36 lines
723 B
Java
36 lines
723 B
Java
|
//: patterns/TemplateMethod.java
|
||
|
// 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();
|
||
|
}
|
||
|
} ///:~
|