I was watching a video regarding design patterns and the youtuber made an example of the builder pattern… I didn’t know about the pattern(there is a reason why I was watching the videos); But I had encounter the same type of problems so what I usually did was to return null to the fields I didn’t had their data.

Is it wrong what I was doing?

At the end the builder will make the object with a null data and realistically it takes the same amount of code…

  • @marsara9
    link
    English
    71 year ago

    So the builder pattern is supposed to solve the problem of: if you have a large number of optional fields that may or may not need to be set to construct your object. Then once the dev has called all of the setters that they require, they call build to fully realize that object.

    Some rules that all builders should follow:

    • All setters SHOULD represent optional parameters. (Or ones that have a default value). If a parameter is required for all instances, include it in the constructor of the Builder itself.
    • All setters SHOULD return a copy of the Builder. This way you can chain calls off of each other.
    • Setters SHOULD do nothing more than store the provided value in a field local to the builder itself and then return itself (or a copy of itself).
    • You MUST expose a .build() method that will return the fully realized object. This method should essentially call the constructor for your target object using all of the parameters, regardless if a setter was called or not. Obviously any value where the setter wasn’t called will be null or some default value.
  • @[email protected]
    link
    fedilink
    English
    31 year ago

    You remind me that I suck at design patterns. IMHO, the patterns are only theoretical, and it doesn’t cover all the details of every language. Sometimes it will be null, empty, or can throw an exception if your team decides that it’s better.

  • @[email protected]
    link
    fedilink
    English
    21 year ago

    If null is a valid value for the field there is no reason why a builder should not construct an object where the field is null.

    The only thing i dislike about the pattern is that a class utilizing the builder to retrieve the object has to know a lot about how the object has to be constructed, however it makes for very readable code imho.