Timeline for Builder pattern: How to verify required fields before runtime
Current License: CC BY-SA 4.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 7, 2021 at 14:41 | answer | added | dragosb | timeline score: 2 | |
| Jan 16, 2019 at 19:27 | vote | accept | Devon | ||
| Jan 16, 2019 at 19:16 | comment | added | alextsc | You can solve this in some languages at compile time by using the type system and phantom types (I'm using Scala, but I know it also works in other languages like Haskell). See Type-safe Builder Pattern in Scala for an example. A short search tells me this also works in Java: gabrielsw.blogspot.com/2012/09/phantom-types-in-java.html | |
| Jan 16, 2019 at 17:53 | answer | added | Esben Skov Pedersen | timeline score: 7 | |
| Jan 16, 2019 at 17:32 | answer | added | whatsisname | timeline score: 9 | |
| Jan 16, 2019 at 17:18 | comment | added | Lutz Prechelt | With 10 mandatory fields, there may be ways to derive good defaults for some of them and offer simpler constructors, too (except when an inflexible language allows only one constructor). If there are no such ways, yes: Then a constructor with 10 parameters is quite acceptable. | |
| Jan 16, 2019 at 16:48 | comment | added | Vincent Savard | You could essentially chain builders to do this. You have a NameModelBuilder with has the method PhoneNumberModelBuilder withName(String name), then PhoneNumberModelBuilder has the method EmailModelBuilder withPhoneNumber(String phoneNumber), etc. until you reach a step where you have all the required field. This has the disadvantage of forcing the order of fields to set, but it would achieve what you want. That being said, this is certainly way too over-engineered, and if you get to this point, I believe you may want to reconsider your design. | |
| Jan 16, 2019 at 15:36 | answer | added | Greg Burghardt | timeline score: 2 | |
| Jan 16, 2019 at 15:24 | comment | added | Devon | So, a few fields could be extracted into objects, but most in this case are singular values with no defaults: Names (object), Role, Date of Birth, Gender, Email, Phone Number (object), Address (object), Office ID, Manager ID. | |
| Jan 16, 2019 at 15:19 | comment | added | Euphoric | I would find it weird that all 10 of them are required. And that there are not specific value objects for things like names or addresses, that can minimize amount of those fields in single object. | |
| Jan 16, 2019 at 15:17 | comment | added | Devon | So if you had a model, representing a db table, that had 10 non-nullable fields, you would put all 10 fields in the constructor, or would you suggest a different pattern? | |
| Jan 16, 2019 at 15:16 | comment | added | Euphoric | You use Builder when you have complicated and varied construction of an object. You don't use builder when there are required fields, only to have "nicer" syntax. | |
| Jan 16, 2019 at 14:59 | history | edited | Devon | CC BY-SA 4.0 | added 163 characters in body |
| Jan 16, 2019 at 14:55 | review | First posts | |||
| Jan 16, 2019 at 17:18 | |||||
| Jan 16, 2019 at 14:51 | history | asked | Devon | CC BY-SA 4.0 |