[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item2. μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ λΉŒλ”λ₯Ό κ³ λ €ν•˜λΌ. (λΉŒλ” νŒ¨ν„΄)

2023. 1. 5. 09:30ㆍJAVA/Effective JAVA

728x90

 

μ•„μ΄ν…œ2. μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ λΉŒλ”λ₯Ό κ³ λ €ν•˜λΌ.  

[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item2. μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ λΉŒλ”λ₯Ό κ³ λ €ν•˜λΌ. (점측적 μƒμ„±μž νŒ¨ν„΄, μžλ°”λΉˆμ¦ˆ νŒ¨ν„΄) (tistory.com)

 

[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item2. μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ λΉŒλ”λ₯Ό κ³ λ €ν•˜λΌ. (점측적 μƒμ„±μž νŒ¨ν„΄, μžλ°”

μ•„μ΄ν…œ2. μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ λΉŒλ”λ₯Ό κ³ λ €ν•˜λΌ. μ•žμ—μ„œ λ°°μ› λ˜ 정적 νŒ©ν„°λ¦¬μ™€ μƒμ„±μžμ—λŠ” μ œμ•½μ΄ μžˆλŠ”λ°, 선택적 λ§€κ°œλ³€μˆ˜κ°€ λ§Žμ„ κ²½μš°μ— μ μ ˆν•˜κ²Œ λŒ€μ‘ν•˜κΈ° μ–΄λ ΅λ‹€λŠ” 점이닀. 예λ₯Ό λ“€

hyejin.tistory.com

μœ„μ˜ μž‘μ„±ν•œ λ‚΄μš©μ—μ„œλŠ” μƒμ„±μžμ— λ§€κ°œλ³€μˆ˜κ°€ λ§Žλ‹€λ©΄ κ³ λ €ν•  수 μžˆλŠ” λ°©μ•ˆμœΌλ‘œ 점측적 μƒμ„±μž νŒ¨ν„΄κ³Ό μžλ°”λΉˆμ¦ˆ νŒ¨ν„΄μ— λŒ€ν•΄μ„œ μ„€λͺ…ν–ˆλ‹€. 

근데 이 λ‘κ°œμ˜ λŒ€μ•ˆμ—λŠ” 각각의 단점이 μžˆμ—ˆλŠ”λ°.. 

 

λ¨Όμ € 점측적 μƒμ„±μž νŒ¨ν„΄μ€ μš°μ„  λ§€κ°œλ³€μˆ˜κ°€ λŠ˜μ–΄λ‚˜λ©΄ ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ±°λ‚˜ 읽고 νŒŒμ•…ν•˜κΈ° λ²ˆκ±°λ‘­λ‹€λŠ” 단점이 μžˆλ‹€. 

μžλ°” 빈즈 νŒ¨ν„΄μ€ μ™„μ „ν•œ 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ©”μ„œλ“œλ₯Ό μ—¬λŸ¬λ²ˆ ν˜ΈμΆœν•΄μ•Όν•˜κ³ , μ™„μ „ν•œ 객체λ₯Ό μƒμ„±ν•˜κΈ° μ „κΉŒμ§€λŠ” 일관성이 λ¬΄λ„ˆμ§„ μƒνƒœκ°€ 될 수 μžˆλ‹€λŠ” 단점이 μžˆλ‹€. 

 

λŒ€μ•ˆ1κ³Ό λŒ€μ•ˆ2κ°€ λͺ¨λ‘ 단점?이 μ‘΄μž¬ν•˜λŠ”λ°.. 3번째 λŒ€μ•ˆμœΌλ‘œ λΉŒλ”νŒ¨ν„΄μ„ ꢌμž₯ν•œλ‹€. 

 

λŒ€μ•ˆ3. λΉŒλ” νŒ¨ν„΄ Builder Pattern 

λΉŒλ” νŒ¨ν„΄μ€ μ•žμ—μ„œμ˜ 점측적 μƒμ„±μž νŒ¨ν„΄μ˜ μž₯점이던 μ•ˆμ •μ„±κ³Ό μžλ°”λΉˆμ¦ˆ νŒ¨ν„΄μ˜ 가독성을 κ²ΈλΉ„ν•œ νŒ¨ν„΄μ΄λ‹€. 

 

λΉŒλ”νŒ¨ν„΄μ€ ν΄λΌμ΄μ–ΈνŠΈλŠ” ν•„μš”ν•œ 객체λ₯Ό 직접 λ§Œλ“œλŠ” λŒ€μ‹ , ν•„μˆ˜ λ§€κ°œλ³€μˆ˜λ§ŒμœΌλ‘œ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•΄ λΉŒλ” 객체λ₯Ό μ–»λŠ”λ‹€. 

그런 λ‹€μŒ λΉŒλ” 객체가 μ œκ³΅ν•˜λŠ” μΌμ’…μ˜ μ„Έν„° λ©”μ„œλ“œλ“€λ‘œ μ›ν•˜λŠ” 선택 λ§€κ°œλ³€μˆ˜λ“€μ„ μ„€μ •ν•œλ‹€. 

그리고 λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” build λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ„œ λΆˆλ³€ 객체λ₯Ό 얻을 수 μžˆλ‹€. 

 

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 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;
    }
}

이 ν΄λž˜μŠ€λŠ” λΆˆλ³€ 클래슀둜, λͺ¨λ“  λ§€κ°œλ³€μˆ˜μ˜ 기본값듀을 ν•œ 곳에 λͺ¨μ•„ 두고, λΉŒλ”μ˜ μ„Έν„° λ©”μ„œλ“œλ“€μ€ λΉŒλ” μžμ‹ μ„ λ°˜ν™˜ν•˜κΈ° λ•Œλ¬Έμ— μ—°μ‡„μ μœΌλ‘œ ν˜ΈμΆœν•  수 μžˆλ‹€. 

-> 이런 방식을 ν”Œλ£¨μ–ΈνŠΈ API(fluent API) ν˜Ήμ€ λ©”μ„œλ“œ 연쇄(method chaining) 이라고 ν•œλ‹€. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90