Sadly, no explanation whatsoever of why this approach might be better than just building the object yourself. The example provided is fairly trivial and doesn’t seem to justify using a builder.
Also, how would you do validation of properties (or even detect incompatibility of some of them)? Surely, because Productcan be created directly, the Product class itself should validate its values. But it also makes sense for the ProductBuilder to have its own validation, right? Would that mean doing the validation twice?
The article tries to sell the pattern as being your best friend, but at the same time doesn’t say how that’s gonna happen.
The main advantage of a builder is you can create an object in multiple steps instead of one step.
During the builder process the object might temporarily be in an invalid state - for example perhaps you can’t easily access the product price. You wouldn’t want price to be an optional value for the actual product class, but it can be optional for the builder (as long as it’s set once build() is called).
Sadly, no explanation whatsoever of why this approach might be better than just building the object yourself. The example provided is fairly trivial and doesn’t seem to justify using a builder.
Also, how would you do validation of properties (or even detect incompatibility of some of them)? Surely, because
Product
can be created directly, theProduct
class itself should validate its values. But it also makes sense for theProductBuilder
to have its own validation, right? Would that mean doing the validation twice?The article tries to sell the pattern as being your best friend, but at the same time doesn’t say how that’s gonna happen.
The main advantage of a builder is you can create an object in multiple steps instead of one step.
During the builder process the object might temporarily be in an invalid state - for example perhaps you can’t easily access the product price. You wouldn’t want price to be an optional value for the actual product class, but it can be optional for the builder (as long as it’s set once build() is called).