lundi 16 mars 2020

Pattern for constructing immutable objects with many properties?

Suppose that you have an object with many properties and you'd like to make it immutable for a couple of reasons. Comes the problem of constructing the object with a crazy constructor with too many parameters.

Let's say that this object has 20 properties, the following answers are quite innovative in the sense that they leverage method chain pattern, however, the resulting object is not immutable:

How many constructor arguments is too many?

https://softwareengineering.stackexchange.com/questions/305313/avoiding-constructors-with-many-arguments

https://softwareengineering.stackexchange.com/questions/302128/how-to-deal-with-constructors-in-large-data-classes

Question:

How can one construct an immutable object with many properties without crazy constructors ?

(crazy: a constructor with too many parameters)

Aucun commentaire:

Enregistrer un commentaire