-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package item2; | ||
|
||
/** | ||
* @author lokie on 2020/06/29 | ||
*/ | ||
public class Calzone extends Pizza { | ||
private final boolean sauceInside; | ||
|
||
public static class Builder extends Pizza.Builder<Builder> { | ||
|
||
private boolean sauceInside = false; | ||
|
||
public Builder sauceInside() { | ||
sauceInside = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
Calzone build() { | ||
return new Calzone(this); | ||
} | ||
|
||
@Override | ||
protected Builder self() { | ||
return this; | ||
} | ||
} | ||
|
||
private Calzone(Builder builder) { | ||
super(builder); | ||
sauceInside = builder.sauceInside; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package item2; | ||
|
||
/** | ||
* @author lokie on 2020/06/29 | ||
*/ | ||
public class Main { | ||
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8) | ||
.calories(100).sodium(35).carbohydrate(27).build(); | ||
|
||
NyPizza nyPizza = new NyPizza.Builder(NyPizza.Size.SMALL) | ||
.addTopping(Pizza.Topping.SAUSAGE).addTopping(Pizza.Topping.ONION).build(); | ||
Calzone calzone = new Calzone.Builder().addTopping(Pizza.Topping.HAM).sauceInside().build(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package item2; | ||
|
||
/** | ||
* @author lokie on 2020/06/29 | ||
*/ | ||
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 { | ||
// 필수 | ||
private final int servingSize; | ||
private final int servings; | ||
|
||
// 필수 아닌 것은 초기화 | ||
private int calories = 0; | ||
private int fat = 0; | ||
private int sodium = 0; | ||
private int carbohydrate = 0; | ||
|
||
public Builder(int servingSize, int servings) { | ||
this.servingSize = servingSize; | ||
this.servings = servings; | ||
} | ||
|
||
public Builder calories(int val) { | ||
this.calories = val; | ||
return this; | ||
} | ||
|
||
public Builder fat(int val) { | ||
this.fat = val; | ||
return this; | ||
} | ||
|
||
public Builder sodium(int val) { | ||
this.sodium = val; | ||
return this; | ||
} | ||
|
||
public Builder carbohydrate(int val) { | ||
this.carbohydrate = val; | ||
return this; | ||
} | ||
|
||
public NutritionFacts build(){ | ||
return new NutritionFacts(this); | ||
} | ||
} | ||
|
||
private NutritionFacts(Builder builder){ | ||
this.servingSize = builder.servingSize; | ||
this.servings = builder.servings; | ||
this.calories = builder.calories; | ||
this.fat = builder.fat; | ||
this.sodium = builder.sodium; | ||
this.carbohydrate = builder.carbohydrate; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package item2; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author lokie on 2020/06/29 | ||
*/ | ||
public class NyPizza extends Pizza { | ||
public enum Size { | ||
SMALL, | ||
MEDIUM, | ||
LARGE, | ||
; | ||
} | ||
|
||
private final Size size; | ||
|
||
public static class Builder extends Pizza.Builder<Builder> { | ||
|
||
private final Size size; | ||
|
||
public Builder(Size size) { | ||
this.size = Objects.requireNonNull(size); | ||
} | ||
|
||
@Override | ||
NyPizza build() { | ||
return new NyPizza(this); | ||
} | ||
|
||
@Override | ||
protected Builder self() { | ||
return this; | ||
} | ||
} | ||
|
||
private NyPizza(Builder builder) { | ||
super(builder); | ||
size = builder.size; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package item2; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author lokie on 2020/06/29 | ||
*/ | ||
public abstract class Pizza { | ||
public enum Topping { | ||
HAM, | ||
MUSHROOM, | ||
ONION, | ||
PEPPER, | ||
SAUSAGE, | ||
; | ||
} | ||
|
||
final Set<Topping> toppings; | ||
|
||
abstract static class Builder<T extends Builder<T>> { | ||
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class); | ||
|
||
public T addTopping(Topping topping) { | ||
toppings.add(Objects.requireNonNull(topping)); | ||
return self(); | ||
} | ||
|
||
abstract Pizza build(); | ||
|
||
protected abstract T self(); | ||
} | ||
|
||
Pizza(Builder<?> builder) { | ||
toppings = builder.toppings.clone(); | ||
} | ||
} |