Created
February 13, 2018 12:49
-
-
Save Arnot/e3bace7227cfb1efe6f8d973a3aa733c to your computer and use it in GitHub Desktop.
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
/** | |
* Usage | |
*/ | |
Test t = Test.create() | |
.withMember(42) | |
.withString("hello") | |
.withString("world") | |
.build(); | |
/** | |
* Implementation | |
*/ | |
public class Test { | |
private final int member; | |
private final List<String> strings; | |
public static Test.Builder create() { | |
return new Test.Builder(); | |
} | |
private Test(Test.Builder builder) { | |
this.member = builder.member; | |
this.strings = builder.strings; | |
} | |
/* | |
* ... | |
* methods/getters | |
* ... | |
*/ | |
private static class Builder { | |
int member; | |
List<String> strings; | |
public Builder() { | |
strings = new ArrayList<>(); | |
} | |
public Builder withMember(int m) { | |
this.member = m; | |
return this; | |
} | |
public Builder withString(String s) { | |
this.strings.add(s); | |
return this; | |
} | |
public Test build() { | |
return new Test(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment