Exercises
Primitive Values/Whole Values
I demonstrated building the "sell one item" feature with primitive values (strings). In doing this, I take advantage of the fact that strings are Value Objects in Java (equals compares the underlying values and not merely whether they are the same object in memory). If you did this, then try building the feature again, this time introducing Whole Value objects right away without even specifying the underlying value! In Java, for example, that means using a Barcode class that doesn't wrap a value. (You could even just use Object on its own, but most people find that too weird. Try it if you dare!) Since there's no underlying value to compare, then Barcode objects (strangely) represent values, but act like "regular" objects (only multiple references to the same object are equal).
- How many tests can you write without needing to specify a value for the underlying Barcode?
- Which feature or test forces you to introduce a value property inside the Barcode?
- Which code, if any, does the Barcode class attract? How does this change the overall design of the system?
- What if you do the same thing with Price?