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]

Unlike C# and C++, Java has no support for custom value types at the language level. Every custom type is a reference type, and therefore has identity and reference semantics,[9] though extending support for custom value types is being considered.[10]

Java programmers therefore emulate value objects by creating immutable objects,[11] because if the state of an object does not change, passing references is semantically equivalent to copying value objects.

A class can be made immutable by declaring all attributes blank final,[12] and declaring all attributes to be of immutable type (such as String, Integer, or any other type declared in accordance with these rules), not of mutable type such an ArrayList. They should also define equals and hashCode to compare values rather than references. 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.[13]

Value Objects in C++[edit]

In C++ a value object can be built by overloading the assignment operator and using appropriate constness constraints on the fields (which will be evaluated once by the initializer list of the constructor) and on 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. ^ "Java Language Specification, chapter 4. Types, Values, and Variables". Retrieved 7 October 2015. 
  10. ^ "JEP 169: Value Objects". Retrieved 7 October 2015. 
  11. ^ "Immutable objects". Collected Java Practices. 2012. Retrieved 5 September 2012. 
  12. ^ hence assignable only in the constructors
  13. ^ "VALJOs - Value Java Objects". Retrieved 19 October 2014.