Pizza Builder Example

Ben Fagin edited this page Oct 31, 2013 · 8 revisions

The Pizza Builder, though a bit contrived, is useful in showcasing a simple builder. The user can select a SauceType, add cheese, and add various Toppings. A maximum of 3 toppings can be added. As well, cheese and sauce can only be added once. When the user is done, they can bake the pizza. The baking process takes time, but when finished yields a delicious Pizza object.

The descriptor:

Descriptor builder = Flapi.builder()

    .addMethod("addSauce( sauceType)").atMost(1)
    .addMethod("addTopping( topping)").atMost(3)

And the corresponding usage:

Pizza myDeliciousPizza = PizzaGenerator.makePizza(new PizzaHelperImpl())

The full source can be found here.