Skip to content

Instantly share code, notes, and snippets.

@phamducminh
Created August 15, 2019 02:29
Show Gist options
  • Save phamducminh/18e3388ca94e42ba69cc5e29fafe8d2d to your computer and use it in GitHub Desktop.
Save phamducminh/18e3388ca94e42ba69cc5e29fafe8d2d to your computer and use it in GitHub Desktop.
Builder pattern sample code
public final class Coordinate {
final int x;
final int y;
public static Builder builder() {
return new Builder();
}
private Coordinate(Builder builder) {
this.x = builder.x;
this.y = builder.y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public static final class Builder {
int x;
int y;
private Builder() {
}
public Builder setX(int x) {
this.x = x;
return this;
}
public Builder setY(int y) {
this.y = y;
return this;
}
public Coordinate build() {
return new Coordinate(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment