#10 effective java: item 2
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.example.effectivejava.chapter01.item02.builder;
|
||||
|
||||
public class BuilderTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new NutritionFacts.Builder(10, 10)
|
||||
.calories(10)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example.effectivejava.chapter01.item02.builder;
|
||||
|
||||
// 코드 2-3 빌더 패턴 - 점층적 생성자 패턴과 자바빈즈 패턴의 장점만 취했다. (17~18쪽)
|
||||
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 void main(String[] args) {
|
||||
NutritionFacts cocaCola = new Builder(240, 8)
|
||||
.calories(100)
|
||||
.sodium(35)
|
||||
.carbohydrate(27).build();
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.effectivejava.chapter01.item02.hierarchicalbuilder;
|
||||
|
||||
// 코드 2-6 칼초네 피자 - 계층적 빌더를 활용한 하위 클래스 (20~21쪽)
|
||||
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 public Calzone build() {
|
||||
return new Calzone(this);
|
||||
}
|
||||
|
||||
@Override protected Builder self() { return this; }
|
||||
}
|
||||
|
||||
private Calzone(Builder builder) {
|
||||
super(builder);
|
||||
sauceInside = builder.sauceInside;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return String.format("%s로 토핑한 칼초네 피자 (소스는 %s에)",
|
||||
toppings, sauceInside ? "안" : "바깥");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.effectivejava.chapter01.item02.hierarchicalbuilder;
|
||||
|
||||
// 코드 2-5 뉴욕 피자 - 계층적 빌더를 활용한 하위 클래스 (20쪽)
|
||||
public class NyPizza extends Pizza {
|
||||
public enum Size { SMALL, MEDIUM, LARGE }
|
||||
private final Size size;
|
||||
|
||||
public static class Builder extends Pizza.Builder<NyPizza.Builder> {
|
||||
private final Size size;
|
||||
|
||||
public Builder(Size size) {
|
||||
this.size = Objects.requireNonNull(size);
|
||||
}
|
||||
|
||||
@Override public NyPizza build() {
|
||||
return new NyPizza(this);
|
||||
}
|
||||
|
||||
@Override protected Builder self() { return this; }
|
||||
}
|
||||
|
||||
private NyPizza(Builder builder) {
|
||||
super(builder);
|
||||
size = builder.size;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return toppings + "로 토핑한 뉴욕 피자";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.effectivejava.chapter01.item02.hierarchicalbuilder;// 코드 2-4 계층적으로 설계된 클래스와 잘 어울리는 빌더 패턴 (19쪽)
|
||||
|
||||
// 참고: 여기서 사용한 '시뮬레이트한 셀프 타입(simulated self-type)' 관용구는
|
||||
// 빌더뿐 아니라 임의의 유동적인 계층구조를 허용한다.
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
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();
|
||||
|
||||
// 하위 클래스는 이 메서드를 재정의(overriding)하여
|
||||
// "this"를 반환하도록 해야 한다.
|
||||
protected abstract T self();
|
||||
}
|
||||
|
||||
Pizza(Builder<?> builder) {
|
||||
toppings = builder.toppings.clone(); // 아이템 50 참조
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.effectivejava.chapter01.item02.hierarchicalbuilder;
|
||||
|
||||
import static com.example.effectivejava.chapter01.item02.hierarchicalbuilder.NyPizza.Size.SMALL;
|
||||
import static com.example.effectivejava.chapter01.item02.hierarchicalbuilder.Pizza.Topping.*;
|
||||
|
||||
// 계층적 빌더 사용 (21쪽)
|
||||
public class PizzaTest {
|
||||
public static void main(String[] args) {
|
||||
NyPizza pizza = new NyPizza.Builder(SMALL)
|
||||
.addTopping(SAUSAGE)
|
||||
.addTopping(ONION).build();
|
||||
|
||||
Calzone calzone = new Calzone.Builder()
|
||||
.addTopping(HAM).sauceInside().build();
|
||||
|
||||
System.out.println(pizza);
|
||||
System.out.println(calzone);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.example.effectivejava.chapter01.item02.javabeans;
|
||||
|
||||
// 코드 2-2 자바빈즈 패턴 - 일관성이 깨지고, 불변으로 만들 수 없다. (16쪽)
|
||||
public class NutritionFacts {
|
||||
// 필드 (기본값이 있다면) 기본값으로 초기화된다.
|
||||
private int servingSize = -1; // 필수; 기본값 없음
|
||||
private int servings = -1; // 필수; 기본값 없음
|
||||
private int calories = 0;
|
||||
private int fat = 0;
|
||||
private int sodium = 0;
|
||||
private int carbohydrate = 0;
|
||||
private boolean healthy;
|
||||
|
||||
public NutritionFacts() { }
|
||||
|
||||
public void setServingSize(int servingSize) {
|
||||
this.servingSize = servingSize;
|
||||
}
|
||||
|
||||
public void setServings(int servings) {
|
||||
this.servings = servings;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public void setFat(int fat) {
|
||||
this.fat = fat;
|
||||
}
|
||||
|
||||
public void setSodium(int sodium) {
|
||||
this.sodium = sodium;
|
||||
}
|
||||
|
||||
public void setCarbohydrate(int carbohydrate) {
|
||||
this.carbohydrate = carbohydrate;
|
||||
}
|
||||
|
||||
public void setHealthy(boolean healthy) {
|
||||
this.healthy = healthy;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
NutritionFacts cocaCola = new NutritionFacts();
|
||||
cocaCola.setServingSize(240);
|
||||
cocaCola.setServings(8);
|
||||
|
||||
cocaCola.setCalories(100);
|
||||
cocaCola.setSodium(35);
|
||||
cocaCola.setCarbohydrate(27);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.example.effectivejava.chapter01.item02.telescopingconstructor;
|
||||
|
||||
// 코드 2-1 점층적 생성자 패턴 - 확장하기 어렵다! (14~15쪽)
|
||||
public class NutritionFacts {
|
||||
private final int servingSize; // (mL, 1회 제공량) 필수
|
||||
private final int servings; // (회, 총 n회 제공량) 필수
|
||||
private final int calories; // (1회 제공량당) 선택
|
||||
private final int fat; // (g/1회 제공량) 선택
|
||||
private final int sodium; // (mg/1회 제공량) 선택
|
||||
private final int carbohydrate; // (g/1회 제공량) 선택
|
||||
|
||||
public NutritionFacts(int servingSize, int servings) {
|
||||
this(servingSize, servings, 0);
|
||||
}
|
||||
|
||||
public NutritionFacts(int servingSize, int servings,
|
||||
int calories) {
|
||||
this(servingSize, servings, calories, 0);
|
||||
}
|
||||
|
||||
public NutritionFacts(int servingSize, int servings,
|
||||
int calories, int fat) {
|
||||
this(servingSize, servings, calories, fat, 0);
|
||||
}
|
||||
|
||||
public NutritionFacts(int servingSize, int servings,
|
||||
int calories, int fat, int sodium) {
|
||||
this(servingSize, servings, calories, fat, sodium, 0);
|
||||
}
|
||||
|
||||
public NutritionFacts(int servingSize, int servings,
|
||||
int calories, int fat, int sodium, int carbohydrate) {
|
||||
this.servingSize = servingSize;
|
||||
this.servings = servings;
|
||||
this.calories = calories;
|
||||
this.fat = fat;
|
||||
this.sodium = sodium;
|
||||
this.carbohydrate = carbohydrate;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
NutritionFacts cocaCola =
|
||||
new NutritionFacts(10, 10);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user