vendredi 24 juillet 2015

Should I create a builder for a Class wich has large number of required attributes

If I have a class with Fairly large number of attributes, say 10, all of which are required [non null or non optional] to construct the entity correctly, then what is the best way to instantiate this class?

  • Should I create a Builder with no args constructor which has withers/setters for all the attributes and check for "non null" in the private constructor of the entity being constructed

    OR

  • should I have a public constructor with all the 10 arguments and create NO Builder?

Having a builder makes the client code look cleaner while having an all args public constructor conveys the message that all the arguments are required [say, I annotate all the parameters with @javax.annotations.Nonnull annotation]

How should I decide?

Aucun commentaire:

Enregistrer un commentaire