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?

+3
java immutables-library


source to share


No one has answered this question yet

Check out similar questions:

2727
What is serialVersionUID and why should I use it?
904
Which @NotNull Java annotation should I use?
828
When and how should I use the ThreadLocal variable?
128
How to copy strings in Java?
17
Should "Comparable <T>" be a "functional interface"?
ten
java immutable class is much slower
five
Immutable classes and subclasses
4
Should I use real objects or mockups in unit tests with Immutables?
3
Using an abstract class instead of an interface in the strategy design pattern
3
Besides immutable value objects, when should I override `equals ()`?



All Articles
Loading...
X
Show
Funny
Dev
Pics