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();