Skip to content

Latest commit

 

History

History
33 lines (20 loc) · 2.6 KB

File metadata and controls

33 lines (20 loc) · 2.6 KB

Value Object

Scenario

In this demo we want to do some fractional arithmetic. A fraction is an example for an entity that represents a defined value on the one hand but has structure on the other: numerator and denominator. Even more interesting: two fractions looking different can be equal, such as: 1/3 == 2/6.

Choice of Pattern

Here we want to apply the Value Object Pattern to introduce a small simple object, like money or date range, whose equality isn't based on identity (Fowler).

Test

A Fraction is an immutable tuple of numerator and denominator.

Test

Every operation on a Fraction instance that would modify it, instead creates a new Fraction instance.

Try it out!

Open ValueObjectTest.java to start playing with this pattern. By setting the log-level for this pattern to DEBUG in logback.xml you can watch the pattern working step by step. Take a look, how equality of Fraction is defined (equals(), hashCode(), compareTo()).

Remarks

References

  • (Fowler) Fowler, M.: Patterns of Enterprise Application Architecture. Addison-Wesley (2002)