-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFluentInheritance.java
More file actions
52 lines (44 loc) · 1.72 KB
/
FluentInheritance.java
File metadata and controls
52 lines (44 loc) · 1.72 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
43
44
45
46
47
48
49
50
51
52
@SuppressWarnings("rawtypes")
interface BuildWithName<S extends BuildWithName> {
S setName(String name);
}
interface BuildWithNameAndDesc extends BuildWithName<BuildWithNameAndDesc> {
BuildWithNameAndDesc setDescription(String description);
}
interface BuildWithNameAndColor extends BuildWithName<BuildWithNameAndColor> {
BuildWithNameAndColor setColor(String description);
}
@SuppressWarnings("rawtypes")
class BuildWithNameImpl<I extends BuildWithName, S extends I> implements BuildWithName<I> {
@SuppressWarnings("unchecked")
protected S self() {
return (S) this;
}
@Override
public S setName(String name) {
System.out.println(String.format("setName(%s)", name));
return self();
}
}
class BuildWithNameAndDescImpl extends BuildWithNameImpl<BuildWithNameAndDesc, BuildWithNameAndDescImpl> implements BuildWithNameAndDesc {
@Override
public BuildWithNameAndDescImpl setDescription(String description) {
System.out.println(String.format("setDescription(%s)", description));
return self();
}
}
class BuildWithNameAndColorImpl extends BuildWithNameImpl<BuildWithNameAndColor, BuildWithNameAndColorImpl> implements BuildWithNameAndColor {
@Override
public BuildWithNameAndColorImpl setColor(String color) {
System.out.println(String.format("setColor(%s)", color));
return self();
}
}
class DemoFluentInheritance {
public static void main(String args[]) {
BuildWithNameAndDesc o1 = new BuildWithNameAndDescImpl();
o1.setName("foo").setDescription("bar");
BuildWithNameAndColor o2 = new BuildWithNameAndColorImpl();
o2.setName("foo").setColor("bar");
}
}