Skip to content

Instantly share code, notes, and snippets.

@chenminhua
Created August 1, 2018 14:41
Show Gist options
  • Save chenminhua/599496cc7ead734df3a274f616669756 to your computer and use it in GitHub Desktop.
Save chenminhua/599496cc7ead734df3a274f616669756 to your computer and use it in GitHub Desktop.
public class Foo {
// immutability
private final int a;
private final int b;
private final int c;
private final int d;
private final int e;
public static class Builder {
private final int a;
private final int b;
private int c = 0;
private int d = 0;
private int e = 0;
public Builder(int a, int b){
this.a = a;
this.b = b;
}
public Builder c(int val) {c = val; return this;}
public Builder d(int val) {d = val; return this;}
public Builder e(int val) {e = val; return this;}
public Foo build() {
return new Foo(this);
}
}
private Foo(Builder builder) {
a = builder.a;
b = builder.b;
...
}
}
Foo foo = new Foo.Builder(10, 2).c(10).d(20).e(30).build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment