Value Objects

0

Category :

It's often useful to represent things as a compound. A 2D coordinate consists of an x value and y value. An amount of money consists of a number and a currency. A date range consists of start and end dates, which themselves can be compounds of year, month, and day.

I run into the question of whether two compound objects are the same. Objects that are equal due to the value of their properties, in this case their x and y coordinates, are called value objects.

In some noon functional languages you will need to override the default equals method with the tests for the values.

One of the nice consequences of value objects is that I don't need to care about whether I have a reference to the same object in memory or a different reference with an equal value.

Aliasing Bug

To avoid aliasing bugs I follow a simple but important rule: value objects should be immutable. If I want to change my object, I create a new object instead. With objects I can usually do this by simply not providing any setting methods.


my thanks to:
https://martinfowler.com/bliki/ValueObject.html

0 comments:

Post a Comment