re: The Builder Pattern in Java, and Dart Cascades VIEW POST


Just a little detail:

Personally, I prefer the following:

Instead of letting the builder call all the getters/setters (or even set the fields) directly, you can create a Pizza constructor that takes a builder instance and gets the values from there (personally, I tend to make the builder a static inner class of the class it instantiates):

private Pizza(PizzaBuilder builder) {...}

This has some slight advantages, most of all that you can keep your Pizza object immutable, if you want to (why would you change the crust after baking it?), because all your members can be final and all your collections can be immutableList/Set/etc.


Exactly, that's an excellent point to make! That's what I was getting at with the ability to make something immutable.

code of conduct - report abuse