DEV Community

Discussion on: Gang of Four Patterns in Kotlin

Collapse
 
lovis profile image
Lovis • Edited

I agree for this particular example, but consider a class with more properties.
You generally don't want a constructor (or any other method) with five or more parameters.

The builder can also do more than just building the object. E.g. validating correct property-combinations, which is not something you should do inside a constructor.

Collapse
 
programmerr47 profile image
Michael

I think one of the problems with your approach is theoretically possibility to have broken data, since constuction of object is separated from setting up field.

Approach with naming constructor parameters or more stable and looks like builder, since all preparations are happen in constructor of object. And if it will fail, you just will not receive a new object.

Thread Thread
 
lovis profile image
Lovis

Sure, but separating construction from representation is the whole point of the Builder pattern.