Skip to content

Instantly share code, notes, and snippets.

@Arnot
Created February 13, 2018 12:49
Show Gist options
  • Save Arnot/e3bace7227cfb1efe6f8d973a3aa733c to your computer and use it in GitHub Desktop.
Save Arnot/e3bace7227cfb1efe6f8d973a3aa733c to your computer and use it in GitHub Desktop.
/**
* 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