3

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?

iehrlich
  • 3,572
  • 4
  • 34
  • 43
TmTron
  • 17,012
  • 10
  • 94
  • 142
  • also asked this directly on their github page: https://github.com/immutables/immutables/issues/599 – TmTron Apr 13 '17 at 13:22
  • The difference doesn't appear that big to me. An `ImmutableValueObject` instance contains a number of `with*`-methods that can be used to create slightly modified copies, but not much else as far as I can see. It just seems cleaner to me to use the `ValueObject` references everywhere, and only invoke the Immutables framework when a new instance must be created. – Henrik Aasted Sørensen Apr 26 '17 at 18:40

0 Answers0