[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ. (๊ณ„์ธตํ˜• ๋นŒ๋”)

2023. 1. 6. 14:30ใ†JAVA/Effective JAVA

728x90

 

์•„์ดํ…œ2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ.  

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ. (๋นŒ๋” ํŒจํ„ด) (tistory.com)

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ. (๋นŒ๋” ํŒจํ„ด)

์•„์ดํ…œ2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ. [์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item2. ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ๋นŒ๋”๋ฅผ ๊ณ ๋ คํ•˜๋ผ. (์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด, ์ž๋ฐ”๋นˆ์ฆˆ ํŒจํ„ด) (tistory.com) [์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Ite

hyejin.tistory.com

์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”์—์„œ๋Š” ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด์ด๋‚˜ ์ž๋ฐ”๋นˆ์ฆˆ ํŒจํ„ด๋ณด๋‹ค๋Š” ๋นŒ๋”ํŒจํ„ด ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. 

 

์ด ๋นŒ๋”ํŒจํ„ด์€ ๊ณ„์ธต์ ์œผ๋กœ ์„ค๊ณ„๋œ ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์“ฐ๊ธฐ์— ์ข‹๋‹ค. 

์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ๋นŒ๋”๋ฅผ, ๊ตฌ์ฒด ํด๋ž˜์Šค๋Š” ๊ตฌ์ฒด ๋นŒ๋”๋ฅผ ๊ฐ–๋Š”๋‹ค. 

 

Pizza : ๊ณ„์ธต ๊ตฌ์กฐ์˜ ๋ฃจํŠธ์— ๋†“์ธ ์ถ”์ƒ ํด๋ž˜์Šค 

pizza๋Š” ์ถ”์ƒ ํด๋ž˜์Šค๋กœ Builder ๋„ ์—ญ์‹œ ์ถ”์ƒ ๋นŒ๋”๋ฅผ ๊ฐ–๋„๋ก ํ–ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์ด Pizza.Builder ํด๋ž˜์Šค๋Š” ์žฌ๊ท€์  ํƒ€์ž… ํ•œ์ •์„ ์ด์šฉํ•˜๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž…์ด๋‹ค. 

 

โ— ์žฌ๊ท€์  ํƒ€์ž… ํ•œ์ • 

: ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ž์‹ ์˜ ํƒ€์ž…์„ ํฌํ•จํ•˜๋Š” ์ƒ์œ„ ๋ชจ๋“ˆ์— ํ•œ์ •๋˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  self()๋ฅผ  ์ถ”๊ฐ€ํ•ด์„œ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ํ˜•๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ ๋„ ๋ฉ”์„œ๋“œ ์—ฐ์‡„๋ฅผ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

NyPizza : Pizza์˜ ํ•˜์œ„ ํด๋ž˜์Šค 

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
      public NyPizza build()
      {
         return new NyPizza(this);
      }
      
      @Override
      protected Builder self()
      {
         return this;
      }
   }
   
   NyPizza(Builder builder)
   {
      super(builder);
      size = builder.size;
   }
   
   @Override
   public String toString()
   {
      return toppings + "๋กœ ํ† ํ•‘ํ•œ ํ”ผ์ž";
   }
}

-> ๋‰ด์š• ํ”ผ์ž๋Š” ํฌ๊ธฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•„์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. 

 

 

Calzone : Pizza์˜ ํ•˜์œ„ ํด๋ž˜์Šค 

public class Calzone extends Pizza
{
   
   private final boolean sauceInside;
   
   public static class Builder extends Pizza.Builder<Builder>
   {
      private boolean sauceInside;
      
      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 ? "์•ˆ" : "๋น„๊นฅ");
   }
}

-> ์นผ์ดˆ๋„ค ํ”ผ์ž๋Š” ์†Œ์Šค๋ฅผ ์•ˆ์— ๋„ฃ์„์ง€ ์„ ํƒํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•„์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. 

 

 

๊ฐ ํ•˜์œ„ ํด๋ž˜์Šค์ธ NyPizza์™€ Calzone ๊ฐ€ ๊ตฌํ˜„ํ•œ build() ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ฉด ํ•ด๋‹นํ•˜๋Š” ๊ตฌ์ฒด ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์„ ์–ธํ•œ๋‹ค. 

NyPizza.Builder๋Š” NyPizza๋ฅผ Calzone.Builder๋Š” Calzone๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

-> ์ด๋ ‡๊ฒŒ ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์œ„ ํด๋ž˜์Šค๊ฐ€ ์ •์˜ํ•œ ๋ฐ˜ํ™˜ ํƒ€์ž…์ด ์•„๋‹Œ, ๊ทธ ํ•˜์œ„ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ณต๋ณ€ ๋ฐ˜ํ™˜ ํƒ€์ดํ•‘์ด๋ผ๊ณ  ํ•œ๋‹ค. 

๊ณต๋ณ€ ๋ฐ˜ํ™˜ ํƒ€์ดํ•‘ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ ํ˜• ๋ณ€ํ™˜์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ณ , ๋นŒ๋”์— ๋„˜๊ธฐ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. 

๋นŒ๋” ํŒจํ„ด์€ ์žฅ์ ๋„ ์žˆ์ง€๋งŒ, ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด์— ๋นŒ๋”๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•˜๋Š”๋ฐ ์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด๋ณด๋‹ค๋Š” ์ฝ”๋“œ๊ฐ€ ์žฅํ™ฉํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 4๊ฐœ ์ด์ƒ์€ ๋˜์–ด์•ผ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. 

 

 

 

๐Ÿ’˜ ์ด ์ •๋ฆฌ๋กœ ์ƒ์„ฑ์ž๋‚˜ ์ •์  ํŒฉํ„ฐ๋ฆฌ๊ฐ€ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 4๊ฐœ์ด์ƒ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด ์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด์ด๋‚˜ ์ž๋ฐ”๋นˆ์ฆˆ ํŒจํ„ด๋ณด๋‹ค๋Š” ๋นŒ๋” ํŒจํ„ด ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค. ํŠนํžˆ!! ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์€ ํƒ€์ž…์ด๋ผ๋ฉด ๋”๋”์šฑ!! 

๋นŒ๋”๋Š” ์ ์ธต์  ์ƒ์„ฑ์ž ํŒจํ„ด๋ณด๋‹ค ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์“ฐ๊ธฐ ํŽธ๋ฆฌํ•˜๊ณ , ์ž๋ฐ”๋นˆ์ฆˆ๋ณด๋‹ค ํ›จ์”ฌ ์•ˆ์ „ํ•˜๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง„ ํŒจํ„ด์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90