Immutables: should I use interface or ImmutableValueObject
Question about using immutables java library.
In their documentation, they seem to ImmutableValueObject
only use to instantiate, and then in code they use ValueObject
(an interface or abstract base class) like this:
ValueObject valueObject =
ImmutableValueObject.builder()
.name("My value")
.addCounts(1)
.addCounts(2)
.build();
Isn't it better to always use ImmutableValueObject
?
eg:.
ImmutableValueObject valueObject =
ImmutableValueObject.builder()
.name("My value")
.addCounts(1)
.addCounts(2)
.build();
Especially for method parameters etc. ValueObject
does not guarantee immutability (for example, someone might just subclass ValueObject
and pass a mutable instance).
Often I want to use an interface because I can just 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?
source to share
No one has answered this question yet
Check out similar questions: