Created
October 22, 2018 17:22
-
-
Save sajjadyousefnia/5afc3ced1ec003599df75b63889f0666 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Builder Pattern | |
public class NutritionFacts { | |
private final int servingSize; | |
private final int servings; | |
private final int calories; | |
private final int fat; | |
private final int sodium; | |
private final int carbohydrate; | |
public static class Builder { | |
// Required parameters | |
private final int servingSize; | |
private final int servings; | |
// Optional | |
private int | |
private int | |
private int | |
private int | |
parameters - initialized to default values | |
calories | |
= 0; | |
fat | |
= 0; | |
sodium | |
= 0; | |
carbohydrate = 0; | |
public Builder(int servingSize, int servings) { | |
this.servingSize = servingSize; | |
this.servings | |
= servings; | |
} | |
public Builder calories(int val) | |
{ calories = val; | |
return this; | |
public Builder fat(int val) | |
{ fat = val; | |
return this; | |
public Builder sodium(int val) | |
{ sodium = val; | |
return this; | |
public Builder carbohydrate(int val) | |
{ carbohydrate = val; return this; | |
public NutritionFacts build() { | |
return new NutritionFacts(this); | |
} | |
} | |
private NutritionFacts(Builder builder) { | |
servingSize = builder.servingSize; | |
servings | |
= builder.servings; | |
calories | |
= builder.calories; | |
fat | |
= builder.fat; | |
sodium | |
= builder.sodium; | |
carbohydrate = builder.carbohydrate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment