forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateMethod.java
More file actions
42 lines (39 loc) · 1.04 KB
/
TemplateMethod.java
File metadata and controls
42 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// patterns/TemplateMethod.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Basic Template Method pattern.
import java.util.stream.*;
abstract class ApplicationFramework {
ApplicationFramework() {
templateMethod();
}
abstract void customize1(int n);
abstract void customize2(int n);
// "private" means automatically "final":
private void templateMethod() {
IntStream.range(0, 5).forEach(
n -> { customize1(n); customize2(n); });
}
}
// Create a new application:
class MyApp extends ApplicationFramework {
@Override void customize1(int n) {
System.out.print("customize1 " + n);
}
@Override void customize2(int n) {
System.out.println(" customize2 " + n);
}
}
public class TemplateMethod {
public static void main(String[] args) {
new MyApp();
}
}
/* Output:
customize1 0 customize2 0
customize1 1 customize2 1
customize1 2 customize2 2
customize1 3 customize2 3
customize1 4 customize2 4
*/