I’ve got a value object, which stores info for example amount. The getAmount() getter returns amount in cents. However in various places, we need to get amount in dollar. There are 2 approaches I can think of:
- write a convert method and place it in a utility class.
- add a getAmountInDollar() getter in the value object.
I prefer the second approach. What do you think? What are pros and cons of both approaches?
That’s a bit a matter of taste. But, in my opinion, if this information is irrelevant to the model in question, then I’d prefer the first approach. It keeps the model clean and the other benefit is that it’s reuseable for all other values of that kind. It would be much nicer as well if you make the currency type another argument of that utility method, this way it’s even more flexible. Hint:
NumberFormat.