Value object

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.[1][2]

Examples of value objects are objects representing an amount of money or a date range.

Being small, one can have multiple copies of the same value object that represent the same entity: it is often simpler to create a new object rather than rely on a single instance and use references to it.[2]

Value objects should be immutable:[3] this is required for the implicit contract that two value objects created equal, should remain equal. It is also useful for value objects to be immutable, as client code cannot put the value object in an invalid state or introduce buggy behaviour after instantiation.[4]

Value objects work better if they have native support for copy-by-value semantics, i.e. the expression

valueObject1 = valueObject2

assigns the value of the valueObject1 by creating a copy of the valueObject2, instead of assigning a reference to the second object, as happens in most object oriented languages for assignments among objects.[5]

Value objects are among the building blocks of DDD.

Value objects in C#[edit]

In C# a class is a reference type while a struct (concept derived from the struct in C language) is a value type.[6] Hence an instance derived from a class definition is an object while an instance derived from a struct definition is said to be a value object (to be precise a struct can made immutable to represent a value object declaring attributes as readonly[7]).

The main differences between objects and value objects in C# are that:

  • objects are mutable
  • value objects are immutable
  • objects are referenced and the expression object1 = object2 assigns to object1 a reference to object2
  • value objects are copied by value (cloned): hence the expression valueObject1 = valueObject2 makes a copy of the values of the attributes of valueObject2 to those of valueObject1.

The memory management is also different: the data of a struct in C# is kept in the stack while for classes the data is stored in the heap memory. This memory management can help in terms of performance. This does not happen when the value in question is wrapped inside another object, e.g. an array of some struct type, in which case the struct(s) will live, inside the object, on the heap.[8]

Value Objects in Java[edit]

When talking about a Value Object in Java language usually one refers to a small JavaBean, a DTO or a POJO.

A Java Bean is a Java class that has getter and setter methods in order to conform to the Java-bean standard.[9]

A DTO in java usually is a serializable Java Bean used to exchange information.[10]

A POJO is a plain old java object and hence an instance of a simple java class.[11]

Hence all these are kinds of mutable Java classes, none of which can be strictly speaking considered a Value object.

In Java it is instead possible to create an immutable object[12] which is close to a Value object: one needs to make the class immutable, by declaring all the attributes blank final[13] and define a copy constructor, and proper hash and equality methods: the object should contain attributes that are derived from immutable classes as well, e.g. a String or an Integer, and not collections or objects with a mutable inner part. The term "VALJO" (VALue Java Object) has been coined to refer to the stricter set of rules necessary for a correctly defined immutable value object.[14]

Still objects derived from this class definition would miss the copy-by-value semantics: i.e., with this class one cannot write valueObject1 = valueObject2, but has to write valueObject1 = new ValueObject(valueObject2) (invoking the copy constructor).

A formal notion of a "value object" may be added to the Java language in the future. JEP169 is the proposal under current consideration.

Value Objects in C++[edit]

In C++ a Value object can be built thanks to the fact that the assignment operator can be overloaded and using appropriate constness constraints to the fields (which will be evaluated once by the initializer list of the constructor) and to the methods of the class.

See also[edit]


  1. ^ Fowler, Martin (2003). "Value Object". Catalog of Patterns of Enterprise Application Architecture. Martin Fowler ( Retrieved 17 July 2011. 
  2. ^ a b "Value Object". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. ( Retrieved 6 September 2012. 
  3. ^ "Value Object Should be Immutable". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. ( Retrieved 6 September 2012. 
  4. ^ Burns, Sam. "The Value of a Value Object". 
  5. ^ "Reference Object". Portland Pattern Repository's Wiki. Cunningham & Cunningham, Inc. ( Retrieved 21 May 2013. 
  6. ^ "Classes and Structs (C# Programming Guide)". Microsoft Developer Network ( 2012. Retrieved 5 September 2012. 
  7. ^ "Creating an immutable value object in C# - Part III - Using a struct". Luca Bolognese's WebLog. 2012. Retrieved 7 September 2012. 
  8. ^ ""The Stack Is An Implementation Detail, Part One". Eric Lippert's Blog. 2012. Retrieved 14 December 2012. 
  9. ^ which historically was introduced to enable object handling in a standard way from an IDE, or later from an Application Server in J2EE
  10. ^ The DTO pattern was used in J2EE in parallel with the DAO pattern, up to the introduction of JPA which replaces Entity beans with annotated POJOs
  11. ^ the term was introduced to mean go back to simple objects and stop using J2EE Entity beans
  12. ^ "Immutable objects". Collected Java Practices. 2012. Retrieved 5 September 2012. 
  13. ^ hence assignable only in the constructors
  14. ^ "VALJOs - Value Java Objects". Retrieved 19 October 2014.