Algo-Sim › Design Patterns

Builder: a pizza step by step

Easy · Design Patterns · step through it in Java, Python and C++ on your own input

Collect optional settings on a mutable builder with chainable setters, then build() validates once and returns an immutable object. No telescoping constructors.

Problem

Build pizzas with a PizzaBuilder: size(s) (1 small, 2 medium, 3 large), cheese() and topping() each set one option and return the builder so calls chain. build() returns the price of a new immutable Pizza, 6 + 2·size + 2 for cheese + 1 per topping, or −1 when no size was chosen. Settings persist between builds.

Constraints

Edge cases to test

Follow-up questions

Why not a constructor with defaults?
Many optional parameters lead to telescoping constructors and unreadable call sites; named setters document themselves.
Where is validation done?
Once, in build(), so the built object is always valid and can be immutable.

Solution

Java

class Pizza {
    private final int size, toppings;   // immutable once built
    private final boolean cheese;

    Pizza(PizzaBuilder b) {
        size = b.size;
        cheese = b.cheese;
        toppings = b.toppings;
    }

    int price() {
        return 6 + 2 * size + (cheese ? 2 : 0) + toppings;
    }
}

class PizzaBuilder {
    int size = 0, toppings = 0;         // size 0: not chosen yet
    boolean cheese = false;

    PizzaBuilder size(int s) { size = s; return this; }
    PizzaBuilder cheese() { cheese = true; return this; }
    PizzaBuilder topping() { toppings++; return this; }

    /** Validates once, builds an immutable Pizza and returns its price (-1 if invalid). */
    int build() {
        if (size == 0) return -1;
        return new Pizza(this).price();
    }
}

Python

class Pizza:
    def __init__(self, b):              # immutable once built
        self.size = b._size
        self.cheese = b._cheese
        self.toppings = b._toppings

    def price(self):
        return 6 + 2 * self.size + (2 if self.cheese else 0) + self.toppings


class PizzaBuilder:
    def __init__(self):
        self._size = 0                  # 0: not chosen yet
        self._cheese = False
        self._toppings = 0

    def size(self, s):
        self._size = s
        return self

    def cheese(self):
        self._cheese = True
        return self

    def topping(self):
        self._toppings += 1
        return self

    # Validates once, builds an immutable Pizza and returns its price (-1 if invalid).
    def build(self):
        if self._size == 0:
            return -1
        return Pizza(self).price()

C++

#include <bits/stdc++.h>
using namespace std;
class Pizza {
    const int size, toppings;
    const bool cheese;

  public:
    Pizza(int size, bool cheese, int toppings)
        : size(size), toppings(toppings), cheese(cheese) {}
    int price() const {
        return 6 + 2 * size + (cheese ? 2 : 0) + toppings;
    }
};
class PizzaBuilder {
    int sizeValue = 0, toppings = 0;
    bool hasCheese = false;

  public:
    PizzaBuilder &size(int s) {
        sizeValue = s;
        return *this;
    }
    PizzaBuilder &cheese() {
        hasCheese = true;
        return *this;
    }
    PizzaBuilder &topping() {
        ++toppings;
        return *this;
    }
    int build() {
        if (sizeValue == 0)
            return -1;
        return Pizza(sizeValue, hasCheese, toppings).price();
    }
};

More Design Patterns problems