this post was submitted on 18 Jul 2023
2 points (100.0% liked)
PHP
33 readers
3 users here now
founded 2 years ago
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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).