DEV Community

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

Collapse
 
fpuffer profile image
Frank Puffer

I probably missed something but how do you make your Pizza immutable in Java? To make all the private attributes of Pizza accessible from PizzaBuilder, they need public setters which in my understanding contradicts immutability.

Well, there is a way to resolve this, which you did not mention in your article: You can make PizzaBuilder an inner class of Pizza. Then is can access the private attributes. It works but I am afraid it often makes your code harder to comprehend.

Another thing that should be mentioned: What you describe is the Builder pattern by Joshua Bloch, not the one by the Gang of Four. There are some similarities between the two as both of them separate object construction from object usage. But basically they are different things.

Collapse
 
jvarness profile image
Jake Varness

That's correct, making your builder an inner class is a good way of accomplishing this!

Collapse
 
inakiarroyo profile image
Iñaki Arroyo

At the moment there is not possible to add inner classes in Dart :(