Interface ValueObject

  • All Superinterfaces:
    Producible
    All Known Subinterfaces:
    DomainEvent

    @DomainValueObject
    public interface ValueObject
    extends Producible
    A value object measures, quantifies or describes something in the domain. A value object has no lifecycle from the domain perspective. As such we don’t need to provide him an identity. Value objects can be created and destroyed at will without any impact.

    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
     }
     
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method 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.
    • Method Detail

      • equals

        boolean equals​(Object other)
        As per Domain-Driven Design semantics, value object equality must be computed on all its attributes.
        Overrides:
        equals in class Object
        Parameters:
        other - other object.
        Returns:
        true if the other object is of the same class as this value object and if all attributes are equals, false otherwise.
      • hashCode

        int hashCode()
        As per Domain-Driven Design semantics, the hash code of a value object must be computed on all its attributes.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code value for this value object.