I want to update multiple fields of a Java object at once to perform a check on the update. My idea looks like this:
SomethingValues values = new SomethingValues();
values.name = "abc";
values.amount = 12;
values.weight = 42.2;
...
something.update(values);
An alternative would be a SomethingValuesBuilder, but I think that the additional code needed for a builder is not necessary here.
If I would use setters …
something.setName("abc"); // would perform check
something.setAmount(12); // would perform check
...
… I’d need to perform the check on each setter instead of only one time for all fields in the update method.
Is there any better idea than updating the domain object (something) with an additional value object (values)?
I usually go for the
SomeObjectType.update(parameters)in which the object is responsible of checking and setting all parameters and eventually throw anIllegalArgumentExceptionin case some of the parameters are not accepted. This way you acheive better readability and encapsulation.