@DomainValueObject public interface ValueObject extends Producible
A value object is immutable, meaning that its state cannot be changed after creation. If you need to change a value object, create a new one derived from the initial one. Value object immutability means that they can be easily shared across the whole system.
A value object describes a conceptual whole. All of its attributes are related to each other and are all participating to the description of the thing.
As entities, value objects should contain behavior that is relevant to them. If the domain concept described by the value object has a behavior, write methods encapsulating it. This behavior must remain side-effect free (not depending upon any mutable state).
The BaseValueObject
class can be used as a base class for domain entities. It already
implements the equals(Object)
and hashCode()
methods properly.
Example:
public class SomeValueObject implements ValueObject { private String attribute1; private String attribute2; public SomeValueObject(String attribute1, String attribute2) { this.attribute1 = attribute1; this.attribute2 = attribute2; } @Override public int hashCode() { // implement based on all attributes } @Override public boolean equals() { // implement based on all attributes } // Other methods }
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
As per Domain-Driven Design semantics, value object equality must be computed on all its
attributes.
|
int |
hashCode()
As per Domain-Driven Design semantics, the hash code of a value object must be computed on all
its attributes.
|
boolean equals(Object other)
Copyright © 2013-2018–2019 SeedStack. All rights reserved.