Created
February 16, 2020 07:49
-
-
Save maxixcom/a4e398f4d04421444006e00ff69a4080 to your computer and use it in GitHub Desktop.
Class & Builder - java
This file contains 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
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