JiwonDev

2-1 ์ƒ์„ฑ์ž๋ฅผ ๋Œ€์‹ ํ•˜๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ

by JiwonDev

# ๊ฐœ์š”

API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ์‚ฌ์šฉ์ž๋ผ๊ณ  ์นญํ•œ๋‹ค.

API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ [๊ทธ API์˜ ํด๋ผ์ด์–ธํŠธ]๋ผ๊ณ  ์นญํ•œ๋‹ค.

 

#1. ์ƒ์„ฑ์ž๋ฅผ ๋Œ€์‹ ํ•˜๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ Factory Method๋ผ๊ณ  ํ•œ๋‹ค.

[โœ” ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š”๊ฒƒ] ๋Œ€์‹ ์—  [โœ” Static Factory Method๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•]์˜ ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•˜์ž.

 

์žฅ์ 

  • ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.
  • ๋ฐ˜ํ™˜ ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  • ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋งค๋ฒˆ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  • ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—๋Š” ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๋‹จ์ 

  • ์ƒ์†์„ ํ•˜๋ ค๋ฉด public์ด๋‚˜ protected ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ˆ ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋งŒ ์ œ๊ณตํ•˜๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ˆ˜ ์—†๋‹ค. (๋ถˆ๋ณ€ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค๊ฒฝ์šฐ ์˜คํžˆ๋ ค ์žฅ์ ์ด ๋ ์ˆ˜ ์žˆ๋‹ค)
  • ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํ™•์ธ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.(์ฃผ์„์„ ๋‹ฌ์ž)

์ฃผ์˜์‚ฌํ•ญ

  • ์ƒ์„ฑ์ž๋งŒ ์žˆ์–ด๋„ ์ถฉ๋ถ„ํ•œ ํด๋ž˜์Šค์— ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐ”๋ณด ์ง“์€ ํ•˜์ง€๋ง์ž.
  • ๋ฉ”์„œ๋“œ ์ด๋ฆ„(of, from...)์— ๋Œ€ํ•œ ๊ทœ์•ฝ์ด ์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ์„œ, ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ™•์‹คํ•œ ์ •์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๐Ÿ“‘ ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•ˆ๋˜๋‚˜์š”? ์™œ ๊ตณ์ด Build()๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋‚˜์š”?)

๋”๋ณด๊ธฐ
// ์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด - ๋” ๋งŽ์€ ์ธ์ž ๊ฐœ์ˆ˜์— ์ž˜ ์ ์‘ํ•˜์ง€ ๋ชปํ•œ๋‹ค.
public class NutritionFacts {
  private final int servingSize;  // (mL)             ํ•„์ˆ˜
  private final int servings;     // (per container)  ํ•„์ˆ˜
  private final int calories;     //                  ์„ ํƒ
  private final int fat;          // (g)              ์„ ํƒ
  private final int sodium;       // (mg)             ์„ ํƒ
  private final int carbohydrate; // (g)              ์„ ํƒ

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

 

๋ถˆํŽธํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ž๋ฐ” Bean ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜์˜€๋‹ค.

// ์ž๋ฐ”๋นˆ ํŒจํ„ด - ์ผ๊ด€์„ฑ ํ›ผ์†์ด ๊ฐ€๋Šฅํ•˜๊ณ , ํ•ญ์ƒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค.
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;

  public NutritionFacts() { }
  // ์„ค์ •์ž(setter)
  public void setServingSize(int val) { servingSize = val; }
  public void setServings(int val) { servings = val; }
  public void setCalories(int val) { calories = val; }
  public void setFat(int val) { fat = val; }
  public void setSodium(int val) { sodium = val; }
  public void setCarbohydrate(int val) { carbohydrate = val; }
}

NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);

 

์ž๋ฐ” Bean์—์„œ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์€ ๋นŒ๋” ํŒจํ„ด๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ๊ฐ์ฒด์ƒ์„ฑ์ดํ›„, Setter๋กœ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์ด ๊นจ์งˆ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐœ์„ ํ•œ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ, ๋‚ด๋ถ€ ํด๋ž˜์Šค Builder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

  1. ํ•„์ˆ˜ ์ธ์ž๋“ค์„ ์ƒ์„ฑ์ž์—(๋˜๋Š” ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ์—) ์ „๋ถ€ ์ „๋‹ฌํ•˜์—ฌ ๋นŒ๋” ๊ฐ์ฒด(Builder object)๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. ๋นŒ๋” ๊ฐ์ฒด์— ์ •์˜๋œ ์„ค์ • ๋ฉ”์„œ๋“œ๋“ค์„ ํ˜ธ์ถœํ•˜์—ฌ ์„ ํƒ์  ์ธ์ž๋“ค์„ ์ถ”๊ฐ€ํ•ด ๋‚˜๊ฐ„๋‹ค.
  3. ๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ฌด๋Ÿฐ ์ธ์ž ์—†์ด build ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ(immutable) ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
    calories(100).sodium(35).carbohydrate(27).build();
// ๋นŒ๋” ํŒจํ„ด
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) {
      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;
  }
}

public static Item1 of(String name, String email) {
  return Item1.builder()
    .name(name)
    .email(email)
    .build();
}
public class Main{
    public void main(){
        //from: ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 1๊ฐœ์ผ ๊ฒฝ์šฐ,
        Dog dog = Dog.from(name);
        
        //of: ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ,
        Dog dog = Dog.of(name, age);
        
        //valueOf: from, of ๋ณด๋‹ค ์ž์„ธํ•œ ๋ฒ„์ „.
        Dog dog = Dog.valueOf(name, age);
        
        //getInstance: ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์ž„์„ ๋ณด์žฅํ•˜์ง€ ์•Š์Œ.
        Dog dog = getInstance(age);
        
        //newInstance: ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜.
        Dog dog = Dog.newIncetance(name, age);
        
        //getType: ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ.
        Dog dog = Animal.getDog(name);
        
        //newType: ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ, ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜.
        Dog dog = Animal.newDog(name);
        
        //getType, newType์˜ ๊ฐ„๊ฒฐํ•œ ๋ฒ„์ „.
        Dog dog = Animal.dog(age);
    }
}

 

@ ์žฅ์ 1-๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ๋” ์ฝ๊ธฐ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ์„ฑ์ž์™€ ๋‹ค๋ฅด๊ฒŒ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด ์žˆ์œผ๋ฏ€๋กœ ํƒ€์ž…์ด ์™„์ „ํžˆ ๊ฐ™๋”๋ผ๋„ ๋‹ค๋ฅธ ๋™์ž‘์„ ํ•˜๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

@ ์žฅ์ 2-ํ˜ธ์ถœ๋  ๋•Œ ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ƒ์„ฑ์ž๋ฅผ Private๋กœ ๋ง‰์•„๋†“๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ •์  ํŒฉํ† ๋ฆฌ ๋ฐฉ์‹์˜ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ํ†ต์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@ ์žฅ์ 3,4-์ƒ์„ฑ์ž๋ฅผ ๋ง‰์Œ์œผ๋กœ์„œ ์ธํ„ฐํŽ˜์ด์Šค์ฒ˜๋Ÿผ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ƒ์„ฑ์ž ์—ญํ• ์„ ๋Œ€์‹ ํ•ด์ฃผ๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ๊น€์œผ๋กœ์„œ, ์ธํ„ฐํŽ˜์ด์Šค์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ž์‹ ์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๊ฐ์ฒด๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด๋†“๊ณ  ํ•˜์œ„ํƒ€์ž…, ์ž์‹๊ฐ์ฒด(๊ตฌํ˜„์ฒด)๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋„ ๋œ๋‹ค.

โžก ๊ตฌํ˜„ํด๋ž˜์Šค๋ฅผ ๊ณต๊ฐœํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์–ด ๊ณต๊ฐœ๋œ API๋ฅผ ์ œํ•œํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค.

// Java8์ด์ „์—๋Š” ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด์—์„œ Static, Default ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์—ˆ๋‹ค.
// ๊ทธ๋ž˜์„œ Collection์„ ์•„๋ž˜์™€ ๊ฐ™์ด ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„ํ•˜์˜€์Œ.
public class Collections {
    // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋ถˆ๊ฐ€
    private Collections() {
    }

    // ์ •์  ํŽ™ํ† ๋ฆฌ ๋ฉ”์„œ๋“œ
    public static <T> List<T> unmodifiableList(List<? extends T> list) {
        return (list instanceof RandomAccess ?
                new UnmodifiableRandomAccessList<>(list) :
                new UnmodifiableList<>(list));
    }
}

// ๊ตฌํ˜„์ฒด : non-public ์ด๋‹ค. ์ƒ์œ„ ํƒ€์ž…์˜ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ ๋Š” ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.
class UnmodifiableList<E> extends UnmodifiableCollection<E> implements List<E> {
        ...
}

 

โžก Java8 ์—์„œ interface ์•ˆ์—์„œ deafult, static ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง€๋ฉด์„œ, ์กฐ๊ธˆ ๋” ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

public interface TestInterface {

    void print();

    static void getLowerType() {
        TestExtendClass testExtendClass = new TestExtendClass();
        testExtendClass.print();
    }
}

// package-private์œผ๋กœ ํ•ด๋‹น ๊ตฌํ˜„์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ์ƒ์œ„ ํƒ€์ž…์ธ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.
class TestImplementsClass implements TestInterface {

    @Override
    public void print() {
        System.out.println("print test");
    }
}

 

@ ์žฅ์ 4- ์‚ฌ์šฉํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด๋‘์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋Š” ์‹œ์ ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ๋ง‰๋Š”๋‹ค.

String s = new String("hello"); // ์‹คํ–‰๋  ๋•Œ ๋งˆ๋‹ค ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.
String s = "hello" // ํ•œ ๊ฐ์ฒด(์ƒ์ˆ˜)๋ฅผ ๋‘๊ณ  ์žฌ์‚ฌ์šฉํ•œ๋‹ค.

boolean isValue = str // ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘๋Š” ๋ฐฉ์‹
Boolean.valueOf(str) // ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ๋ฐฉ์‹

ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋กœ ์‹ฑ๊ธ€ํ†ค๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. โžก ์™„๋ฒฝํ•œ ์‹ฑ๊ธ€ํ†ค์„ ์›ํ•œ๋‹ค๋ฉด ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

2021.09.10 - [Backend/๋””์ž์ธํŒจํ„ด] - Singleton (์‹ฑ๊ธ€ํ†ค ํŒจํ„ด)

 

Singleton (์‹ฑ๊ธ€ํ†ค ํŒจํ„ด)

# ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด ํ”„๋กœ์„ธ์Šค์— ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹จ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ํŒจํ„ด. โญ• Static ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ, ์žฌ์‚ฌ์šฉํ•จ์œผ๋กœ์„œ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ๋ฐฉ์ง€ํ•œ๋‹ค. โญ• ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜

jiwondev.tistory.com

public class Person {
  private final Date birthDate;
  // ๋‹ค๋ฅธ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ, ์ƒ์„ฑ์ž๋Š” ์ƒ๋žต

  // ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค!
  public boolean isBabyBoomer() {
    // ์ƒ์„ฑ ๋น„์šฉ์ด ๋†’์€ Date ๊ฐ์ฒด๋ฅผ ์“ธ๋ฐ์—†์ด ์ƒ์„ฑํ•œ๋‹ค
    Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
    Date boomStart = gmtCal.getTime();
    gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
    Date boomEnd = gmtCal.getTime();
    return birthDate.compareTo(boomStart) >= 0 && birthDate.compareTo(boomEnd) < 0;
  }
}
public class Person {
  private final Date birthDate;
  // ๋‹ค๋ฅธ ํ•„๋“œ์™€ ๋ฉ”์„œ๋“œ, ์ƒ์„ฑ์ž๋Š” ์ƒ๋žต

  static {
    Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
    BOOM_START = gmtCal.getTime();
    gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
    BOOM_END = gmtCal.getTime();
  }

  public boolean isBabyBoomer() {
    return birthDate.compareTo(BOOM_START) >= 0 && birthDate.compareTo(BOOM_END) < 0;
  }
}

 


# ์‹ค์ œ ์„œ๋น„์Šค์—์„œ์˜ ์žฅ๋‹จ์ 

์žฅ์ 

  • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ, ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ๋งค์šฐ ์‰ฝ๋‹ค.
  • setter๋ฅผ ์ด์šฉํ•œ ๋ฐฉ์‹(์ž๋ฐ”๋นˆ์ฆˆ)์˜ ๊ฒฝ์šฐ, ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. public์œผ๋กœ setter๊ฐ€ ๊ณต๊ฐœ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด์— ๋น„ํ•ด, ๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ธฐ ์‰ฝ๋‹ค.
  • ๋นŒ๋” ํŒจํ„ด์€ ๊ณ„์ธต์ ์œผ๋กœ ์„ค๊ณ„๋œ ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์“ฐ๊ธฐ์— ์ข‹๋‹ค.

 

๋‹จ์ 

  • ๋นŒ๋” ํŒจํ„ด์€ ์ฝ”๋“œ๋Ÿ‰์ด ๋งŽ๋‹ค. : @builder๋Š” ์–ด๋–ค๊ฐ€, API๋Š” ์‹œ๊ฐ„์ด ์ง€๋‚ ์ˆ˜๋ก ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์•„ ์งˆ๊ฒƒ์ด๋‹ค.
  • ๋นŒ๋”ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค : ์„ฑ๋Šฅ์— ๋งค์šฐ ๋ฏผ๊ฐํ•˜๋‹ค๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ ์ˆ˜๋„ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€๋„ ์•Š๋Š” ๊ณณ์— ๋นŒ๋”๋ฅผ ๋„ฃ์ง€๋ง์ž.

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ