DEV Community

Discussion on: The Builder Pattern in Java, and Dart Cascades

Collapse
 
inakiarroyo profile image
Iñaki Arroyo • Edited

A possible improvement would be adding a named constructor 'Pizza.builder()' which not allow consumers to directly create an instance through the default constructor, giving to the consumer the context that the class is following the builder pattern.

Pizza pizza = Pizza.builder()
..toppings = ['pepperoni', 'mushrooms']
..sauce = 'spaghetti'
..hasExtraCheese = true;

I've written a post following your Pizza example about my own implementation of the Builder pattern :)