Skip to content

Instantly share code, notes, and snippets.

@maxixcom
Created February 16, 2020 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxixcom/a4e398f4d04421444006e00ff69a4080 to your computer and use it in GitHub Desktop.
Save maxixcom/a4e398f4d04421444006e00ff69a4080 to your computer and use it in GitHub Desktop.
Class & Builder - java
public class Product {
private final String prop1;
private final String prop2;
private Product(Builder builder) {
this.prop1 = builder.prop1;
this.prop2 = builder.prop2;
}
// ...
public static Builder newBuilder() {
return new Builder();
}
public static final class Builder {
private String prop1="default_1";
private String prop2="default_2";
public Product build() {
return new Product(this);
}
public Builder setProp1(String prop1) {
this.prop1 = prop1;
return this;
}
public Builder setProp2(String prop2) {
this.prop2 = prop2;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment