πŸ“š Study/Design Pattern

[Effective JAVA] λΉŒλ” νŒ¨ν„΄ - μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žμ„ 경우 μΆ”μ²œ

kongmi 2023. 2. 12. 19:38

λΉŒλ” νŒ¨ν„΄(Builder pattern)

  • ν΄λΌμ΄μ–ΈνŠΈλŠ” ν•„μš”ν•œ 객체λ₯Ό 직접 λ§Œλ“œλŠ” λŒ€μ‹ , ν•„μˆ˜ λ§€κ°œλ³€μˆ˜λ§ŒμœΌλ‘œ μƒμ„±μž(ν˜Ήμ€ 정적 νŒ©ν† λ¦¬)λ₯Ό ν˜ΈμΆœν•΄ λΉŒλ” 객체λ₯Ό μ–»λŠ”λ‹€.
  • λΉŒλ” 객체가 μ œκ³΅ν•˜λŠ” μΌμ’…μ˜ setter λ©”μ†Œλ“œλ“€λ‘œ μ›ν•˜λŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ„€μ •ν•œλ‹€.
  • λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” build λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄ μš°λ¦¬μ—κ²Œ ν•„μš”ν•œ 객체λ₯Ό μ–»λŠ”λ‹€.

πŸ’‘ λΉŒλ”λŠ” 생성할 클래슀 μ•ˆμ— 정적 멀버 클래슀둜 λ§Œλ“€μ–΄λ‘”λ‹€.

πŸ’‘ 점측적 μƒμ„±μž νŒ¨ν„΄λ³΄λ‹€ μ½”λ“œκ°€ μž₯ν™©ν•˜κΈ° λ•Œλ¬Έμ— λ§€κ°œλ³€μˆ˜κ°€ 4개 이상은 λ˜μ–΄μ•Ό μ“Έλ§Œν•¨.

package λΉŒλ”νŒ¨ν„΄;

public class NutritionFacts {
    // ν•„μˆ˜ μž…λ ₯
    private final int servingSize; // 1회 μ œκ³΅λŸ‰
    private final int servings; // 총 n회 μ œκ³΅λŸ‰
    // 선택 μž…λ ₯
    private final int calories; // 1회 μ œκ³΅λŸ‰λ‹Ή
    private final int fat; // μ§€λ°©
    private final int carbohydrage; // νƒ„μˆ˜ν™”λ¬Ό
    private final int protein; // λ‹¨λ°±μ§ˆ

    public static class Builder {
        private final int servingSize;
        private final int servings;

        private int calories = 0;
        private int fat = 0;
        private int carbohydrage = 0;
        private int protein = 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 carbohydrage(int val) {
            carbohydrage = val;
            return this;
        }
        public Builder protein(int val) {
            protein = 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;
        carbohydrage = builder.carbohydrage;
        protein = builder.protein;
    }

    public void viewInfo() {
        System.out.println("===== μ„±λΆ„ν‘œ =====");
        System.out.println("1회 μ œκ³΅λŸ‰ : " + servingSize + "ml");
        System.out.println("총 μ œκ³΅λŸ‰ : " + servings + "회");
        System.out.println("칼둜리 : " + calories + "kcal");
        System.out.println("μ§€λ°© : " + fat + "g");
        System.out.println("νƒ„μˆ˜ν™”λ¬Ό : " + carbohydrage + "g");
        System.out.println("λ‹¨λ°±μ§ˆ : " + protein + "g");
    }
}
package λΉŒλ”νŒ¨ν„΄;

public class Main {
    public static void main(String[] args) {
        NutritionFacts n1 = new NutritionFacts.Builder(240, 8).calories(420)
                .fat(35).carbohydrage(9).protein(2).build();

        n1.viewInfo();
    }
}
===== μ„±λΆ„ν‘œ =====
1회 μ œκ³΅λŸ‰ : 240ml
총 μ œκ³΅λŸ‰ : 8회
칼둜리 : 420kcal
μ§€λ°© : 35g
νƒ„μˆ˜ν™”λ¬Ό : 9g
λ‹¨λ°±μ§ˆ : 2g
  • λΉŒλ”μ˜ setter λ©”μ†Œλ“œλ“€μ€ λΉŒλ” μžμ‹ μ„ λ°˜ν™˜(this)ν•˜κΈ° λ•Œλ¬Έμ— μ—°μ‡„μ μœΌλ‘œ 호좜 κ°€λŠ₯
    (ν”Œλ£¨μ–ΈνŠΈ API, λ©”μ†Œλ“œ 연쇄)
  • λ©”μΈμ—μ„œ 객체 μƒμ„±μ‹œ μž‘μ„±ν•˜λŠ” μ½”λ“œλŠ” μ“°κΈ° 쉽고, 읽기 쉽닀.
아직 클래슀 μ•ˆμ— 정적 클래슀λ₯Ό λ„£μ–΄μ„œ ν˜ΈμΆœν•˜λŠ” 방식이 이해가 100% λ˜μ§€ μ•Šμ•„μ„œ.. 쑰금 더 곡뢀해봐야 ν•  것 κ°™λ‹€!
return thisκ°€ λ‚΄κ°€ μƒκ°ν•œ μ˜λ―Έκ°€ λ§žλŠ”μ§€λ„ 확인 해보고 μ•„λž˜μ— μΆ”κ°€ ν•΄μ•Ό ν•  것 κ°™λ‹€.