# 빌더 패턴

---

빌더 패턴은 동일한 구성 프로세스가 다른 객체를 만들 수 있도록 복잡한 객체 구성을 표현에서 분리한다.

요구사항이 계속 변하는 상황에서 옵셔널하게 프로퍼티를 설정할 때 사용하면 좋다.

```
class Pizza {
  constructor() {
    this.toppings = [];
  }
  
  addTopping(topping) {
    this.toppings.push(topping);
  }
  
  describe() {
    console.log(`This pizza has ${this.toppings.length} toppings: ${this.toppings.join(', ')}.`);
  }
}

class PizzaBuilder {
  constructor() {
    this.pizza = new Pizza();
  }
  
  cheese() {
    this.pizza.addTopping('cheese');
    return this;
  }
  
  pepperoni() {
    this.pizza.addTopping('pepperoni');
    return this;
  }
  
  mushrooms() {
    this.pizza.addTopping('mushrooms');
    return this;
  }
  
  build() {
    return this.pizza;
  }
}

const pizza = new PizzaBuilder()
  .cheese()
  .pepperoni()
  .mushrooms()
  .build();

pizza.describe();
```

For the site tree, see the [root Markdown](https://slashpage.com/develop.md).
