A value object is an object that is compared by value, not by identity. Dates and money are better represented with value objects, whereas accounts or orders are better represented with reference objects.
There are two requirements for a value object:
Here's an example of value object class:
public final class Number { private final int value; public Number(int value) { this.value = value; } public boolean equals(Object o){ if (o == this) return true; if(!(o instanceof Number)) return false; Number other = (Number) o; return this.value == other.value; } public int hashCode(){ int result = 17; result = 31 * result + this.value; return result; } public int getValue() { return value; } }
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.