Start learning free

LESSON 317 OF 343 · DESIGN PATTERNS

Builder Pattern in JavaScript

Practice Creational

Some objects have many parts, and most of them are optional. A constructor that takes every part needs a long list of arguments. That list is easy to get wrong.

The Builder pattern puts the object together one step at a time instead. A separate builder keeps the choices made so far and fills in defaults for the rest. When the choices are done, it hands back the finished object.

Here you write two classes for a pizza shop's order screen. A PizzaBuilder collects one order, and its build() makes the Pizza.

First write Pizza:

  • new Pizza(size, crust, toppings) keeps a size, a crust and a list of toppings. The toppings stay in the order they were added.
  • price() returns the price in cents. Size S costs 800, M costs 1000 and L costs 1200. A thick crust adds 150, and a thin one adds nothing. Each topping adds 120.
  • describe() returns the size, the crust and the toppings, with a space between them. The toppings are joined by "+", as in "L thick cheese+ham".
  • With no topping, the word "plain" takes their place, as in "M thin plain".

Then write PizzaBuilder:

  • new PizzaBuilder() starts with size M, crust thin and no toppings.
  • size(s) sets the size. crust(c) sets the crust.
  • topping(t) adds one more topping. The same topping may come twice, and then it counts twice.
  • These three return the builder itself. So calls can be chained, as in builder.size("L").topping("ham").
  • build() returns a new Pizza with the builder's current choices. The builder keeps them. More calls and another build() make another pizza.

A pizza never changes after it is built, whatever happens to the builder later. So build() gives each new pizza its own copy of the toppings.

The tests never call the constructor of Pizza. They only make builders, call their methods, and keep every pizza that build() returns. Each line of the input is one command:

  • new makes a new PizzaBuilder. The old one is dropped.
  • size S, crust thick and topping ham call size("S"), crust("thick") and topping("ham"). The tests go on with the builder that the call returns.
  • build calls build(). It writes the new pizza's describe() and price() on one line, as in "L thick cheese+ham 1590".
  • show 2 writes pizza 2 the same way. Pizzas are numbered from 1, in the order they were built.

Each test case starts with no pizzas, and its first command is new.

 

Example 1:

Input:
1. new
2. build
3. size L
4. crust thick
5. topping cheese
6. topping ham
7. build
Output:
M thin plain 1000
L thick cheese+ham 1590
Explanation:
Command 2 builds right away, so the pizza gets the defaults M and thin.
Pizza 2 costs 1200 for L, 150 for thick and 120 for each topping.
That is 1200 + 150 + 240 = 1590.

Example 2:

Input:
1. new
2. topping ham
3. build
4. topping ham
5. crust thick
6. build
7. show 1
Output:
M thin ham 1120
M thick ham+ham 1390
M thin ham 1120
Explanation:
Ham is added twice, so pizza 2 has it twice.
It costs 1000 + 150 + 2 * 120 = 1390.
Commands 4 and 5 change the builder, not pizza 1.
So pizza 1 is still M thin ham.

Example 3:

Input:
1. new
2. size S
3. topping olive
4. build
5. new
6. build
7. show 1
Output:
S thin olive 920
M thin plain 1000
S thin olive 920
Explanation:
Command 5 drops the builder and makes a new one.
The new builder starts from the defaults again.
Pizza 1 doesn't change.

 

Constraints:

  • A test case has at most 100,000 commands.
  • A size is S, M or L.
  • A crust is thin or thick.
  • A topping's name has 1 to 12 lowercase letters.
  • One builder gets at most 10 toppings.
  • A show command asks only for a pizza already built.

Note:

Each method should run in O(1) time. A builder holds at most 10 toppings, so copying them takes O(1) time too.

STEP 1 OF 22
    Ctrl + Enter to check

    Builder Pattern in JavaScript

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free