jeudi 24 août 2017

Is it better Step Builder pattern or a Builder pattern with overload constructor for mandatory parameters?

Is it better Step Builder pattern or a Builder pattern with overload constructor for mandatory parameters?

Hi, guys, I have the following scenario, I should do a refactor in a method, where the method receive a new parameter, in this case the method have 4 parameters, and some cases one of this parameters, it is sending null, and 3 are mandatory. I don't want to use in method with 4 parameters by readability, I think use a Builder for this case to build a Object like argument, and the method has 3 method mandatory, I have two alternatives:

1. Use a builder with overload constructor, like this:

new Builder(param1, param2, param3).text("hello").build()
new Builder(param1, param2, param3).build()

2. Use a Step Builder, where the builder can create the object only if the 3 parameter mandatory were implemented, another case it can't build the object. like this:

new Builder().param1(param1).param2(param2).param3(param3).text("hello").build()
new Builder().param1(param1).param2(param2).param3(param3)..build()

But never it can't build the object if it don't implement the 3 parameters mandatory:

new Builder().param1(param1).param2(param2).build() ( invalid execution in IDE)

what is the better option?

thank for your attention.

Aucun commentaire:

Enregistrer un commentaire