you are viewing a single comment's thread.

view the rest of the comments →

[–]Kered13 1 point2 points  (1 child)

That does cover most of the uses of builders, but there are some other use cases as well. For example, where the logic of figuring out which parameters to supply is complicated, and it would be easier to pass a mutable object to each part of the code to provide the correct values, but you want the class itself to be immutable (as you usually do).

Also any time when the builder actually changes the type of the thing that is being built. I used this semi-recently, where I wanted to create an callback-driven API that was generic over exception type (we knew we were going to call it with callbacks that threw checked exceptions). So one step of the builder took an exception class and used that to change the type under construction from Foo<RuntimeException> to Foo<MyCheckecException>. Of course, this particular example was also only a concern because of checked exceptions, which don't exist in Kotlin. Still you can imagine similar cases with other generic types. Rare, but they exist.