Value Objects
Dhaval Dalal
https://dhavaldalal.wordpress.com
@softwareartisan
Value Object
=
Value
+
Object
i.e., Object havingValue Semantics
Example ofValues
• 5
• INR5
• 5%
• 5MB
Properties of Values
• Values model abstractions (concepts) from
problem domain.
• Values have no life-cycle (they don’t exist in
time, they are eternal)
• Values can only be interpreted as they
remain unaltered.
• Values are referentially transparent.
Properties of Objects
• Objects are representations of phenomena from a
problem domain.
• Objects have a life cycle (i.e., they exist in time, can
be instantiated, changed, and deleted).
• Objects have identity that unambiguously denotes
them (thus, they can be referenced).
• Objects can be shared, which is a consequence of
that objects can be referenced.
Combining these properties
• We get Immutability on Objects.
• Why to cherish immutability?
• As there is no modification of internal state, they are
intrinsically thread-safe, and hence don’t require any
synchronisation. Thus, they are painless to work in a
concurrent environment.
• As they are immutable, instances can be shared and
reused across entire application (using Flyweight Design
Pattern).
• Easy to verify correctness of the operations as no internal
state is altered (they behave like pure functions)
A word of warning!
• In OO languages like Java, C#, Groovy, Scala etc…
• Resist all temptation to mutate object’s internal state!
• For example, removing ‘final’ or ‘sealed’ attributes on fields. In Scala avoid changing
a ‘val’ to a ‘var’. In Javascript, use ‘const’ instead of ‘var’.
• Any operation on an Immutable object must produce a new
Immutable object with the new state. Don’t make it a mutable object
any cost!
• In purely FP languages, you don’t have that scope, e.g in
Haskell or Erlang, value once assigned can never change.
• However, in languages that are embracing the FP route or
embrace both paradigms (OO and FP), mutability and
immutability co-exist.
• In Scala, implementValue Object using ‘case’ classes.
References
• Values in Object Systems
• Dirk Baumer, Dirk Riehele, et al.
• The CHECKS pattern language of Information
Integrity.
• Ward Cunningham, James O. Coplien and Doug Schmidt.
• Programming Concurrency on the JVM
• Venkat Subramaniam

Value Objects

  • 1.
  • 2.
  • 3.
    Example ofValues • 5 •INR5 • 5% • 5MB
  • 4.
    Properties of Values •Values model abstractions (concepts) from problem domain. • Values have no life-cycle (they don’t exist in time, they are eternal) • Values can only be interpreted as they remain unaltered. • Values are referentially transparent.
  • 5.
    Properties of Objects •Objects are representations of phenomena from a problem domain. • Objects have a life cycle (i.e., they exist in time, can be instantiated, changed, and deleted). • Objects have identity that unambiguously denotes them (thus, they can be referenced). • Objects can be shared, which is a consequence of that objects can be referenced.
  • 6.
    Combining these properties •We get Immutability on Objects. • Why to cherish immutability? • As there is no modification of internal state, they are intrinsically thread-safe, and hence don’t require any synchronisation. Thus, they are painless to work in a concurrent environment. • As they are immutable, instances can be shared and reused across entire application (using Flyweight Design Pattern). • Easy to verify correctness of the operations as no internal state is altered (they behave like pure functions)
  • 7.
    A word ofwarning! • In OO languages like Java, C#, Groovy, Scala etc… • Resist all temptation to mutate object’s internal state! • For example, removing ‘final’ or ‘sealed’ attributes on fields. In Scala avoid changing a ‘val’ to a ‘var’. In Javascript, use ‘const’ instead of ‘var’. • Any operation on an Immutable object must produce a new Immutable object with the new state. Don’t make it a mutable object any cost! • In purely FP languages, you don’t have that scope, e.g in Haskell or Erlang, value once assigned can never change. • However, in languages that are embracing the FP route or embrace both paradigms (OO and FP), mutability and immutability co-exist. • In Scala, implementValue Object using ‘case’ classes.
  • 8.
    References • Values inObject Systems • Dirk Baumer, Dirk Riehele, et al. • The CHECKS pattern language of Information Integrity. • Ward Cunningham, James O. Coplien and Doug Schmidt. • Programming Concurrency on the JVM • Venkat Subramaniam