Created
July 20, 2016 19:22
-
-
Save phyous/825743cc88812a185357db9d2c38683e to your computer and use it in GitHub Desktop.
Chained interface builder with inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.phyous.cib; | |
public class Cain { | |
public static void main(String[] args) { | |
B b = new B.Builder(). | |
parent( | |
new A.Builder() | |
.m1(1) | |
.m2(2) | |
.o1(3) | |
.o2(4) | |
) | |
.m3(5) | |
.o3(6) | |
.build(); | |
System.out.println(b.toString()); | |
C c = | |
new C.Builder().parent(new B.Builder(). | |
parent( | |
new A.Builder() | |
.m1(1) | |
.m2(2) | |
.o1(3) | |
.o2(4) | |
) | |
.m3(5) | |
.o3(6)) | |
.m4(7) | |
.o4(8) | |
.build(); | |
System.out.println(c.toString()); | |
} | |
static class A { | |
int m1; | |
int m2; | |
int o1; | |
int o2; | |
@Override | |
public String toString() { | |
return String.format("m1: %d m2: %d o1: %d o2: %d", m1, m2, o1, o2); | |
} | |
static class Builder implements BuildM1, BuildM2 { | |
int m1; | |
int m2; | |
int o1; | |
int o2; | |
// Used for concrete classes | |
A build() { | |
A a = new A(); | |
this.enhance(a); | |
return a; | |
} | |
<T extends A> void enhance(T input) { | |
input.m1 = this.m1; | |
input.m2 = this.m2; | |
input.o1 = this.o1; | |
input.o2 = this.o2; | |
} | |
@Override | |
public BuildM2 m1(int m1) { | |
this.m1 = m1; | |
return this; | |
} | |
@Override | |
public Builder m2(int m2) { | |
this.m2 = m2; | |
return this; | |
} | |
public Builder o1(int o1) { | |
this.o1 = o1; | |
return this; | |
} | |
public Builder o2(int o2) { | |
this.o2 = o2; | |
return this; | |
} | |
} | |
interface BuildM1 { | |
BuildM2 m1(int m1); | |
} | |
interface BuildM2 { | |
Builder m2(int m2); | |
} | |
} | |
static class B extends A { | |
int m3; | |
int o3; | |
@Override | |
public String toString() { | |
return super.toString() + String.format(" m3: %d o3: %d", m3, o3); | |
} | |
static class Builder extends A.Builder implements BuildParent, BuildM3 { | |
int m3; | |
int o3; | |
A.Builder parentBuilder; | |
@Override | |
public Builder m3(int m3) { | |
this.m3 = m3; | |
return this; | |
} | |
public Builder o3(int o3) { | |
this.o3 = o3; | |
return this; | |
} | |
@Override | |
public BuildM3 parent(A.Builder a) { | |
parentBuilder = a; | |
return this; | |
} | |
public B build() { | |
B b = new B(); | |
this.enhance(b); | |
return b; | |
} | |
<T extends B> void enhance(T input) { | |
parentBuilder.enhance(input); | |
input.m3 = m3; | |
input.o3 = o3; | |
} | |
} | |
interface BuildParent { | |
BuildM3 parent(A.Builder a); | |
} | |
interface BuildM3 { | |
Builder m3(int m3); | |
} | |
} | |
static class C extends B { | |
int m4; | |
int o4; | |
@Override | |
public String toString() { | |
return super.toString() + String.format(" m4: %d o4: %d", m4, o4); | |
} | |
static class Builder extends B.Builder implements BuildParent, BuildM4 { | |
int m4; | |
int o4; | |
B.Builder parentBuilder; | |
@Override | |
public Builder m4(int m4) { | |
this.m4 = m4; | |
return this; | |
} | |
public Builder o4(int o4) { | |
this.o4 = o4; | |
return this; | |
} | |
@Override | |
public BuildM4 parent(B.Builder b) { | |
parentBuilder = b; | |
return this; | |
} | |
public C build() { | |
C c = new C(); | |
this.enhance(c); | |
return c; | |
} | |
<T extends C> void enhance(T input) { | |
parentBuilder.enhance(input); | |
input.m4 = m4; | |
input.o4 = o4; | |
} | |
} | |
interface BuildParent { | |
BuildM4 parent(B.Builder a); | |
} | |
interface BuildM4 { | |
Builder m4(int m4); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment