The question is about using the immutables java library.
In their documentation they seem to use the ImmutableValueObject
only to construct the instance and then in the code they use the ValueObject
(interface or abstract base class) like this:
ValueObject valueObject =
ImmutableValueObject.builder()
.name("My value")
.addCounts(1)
.addCounts(2)
.build();
Wouldn't it be better to always just use the ImmutableValueObject
?
e.g.:
ImmutableValueObject valueObject =
ImmutableValueObject.builder()
.name("My value")
.addCounts(1)
.addCounts(2)
.build();
Especially for method parameters, etc. the ValueObject
does not guarantee immutability (e.g. someone could just subclass the ValueObject
and pass a mutable instance).
Often, I want to use an interface, because I can simply use a different implementation in my tests. But in the case of an immutable object (which is just a data-object), I don't need this: in the test I just prepare my object the way it should be.
Are there any other reasons, I am missing?