Question

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:

  1. write a convert method and place it in a utility class.
  2. 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?

Was it helpful?

Solution

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.

OTHER TIPS

I think it might be better to generalize your getter with an overload that indicates what units, so I could call getAmount() to get the default, but getAmount(Units.Dollar) or getAmount(Units.Euro) would also be available without having to create a new getter for every possible currency conversion.

Of course this generalizes even further so you could have a temperature value stored internally in Kelvins, but could allow getAmount(Units.Celsius) or getAmount(Units.Rankine) to get the temperature in other scales.

To be honest I'm struggling with this because I think money ought to always be represented as a decimal, used as a decimal internally, then formatted as needed on output. So I'd probably handle the dollar/cents issue in the output formatter.

If I needed to handle conversions to other monetary units, then I'd create a Money class and return it as that with the Money class containing the amount and unit information (and presumably a connection to a service to be used for conversions as needed).

I prefer to keep the public API of a class fairly focused. Once you start adding methods to it that are not part of the "core" you run the risk of having a very complex beast. In the case of a class that pretty much just holds data I would strive to keep it that way.

In the end there is no clear cut "best" answer... it just goes based on your personal experience. Mine says once you start "polluting" the API it is hard to stop, and eventually you need to break the class up anyways.

The utility class also gives you a place to put other related, but not quite, methods that you will probably find over time.

If you goal is to reduce the number of classes, I would say not to. If you feel that having the method in the class makes the code "cleaner" than do it that way.

Because encapsulation is about data and behaviour in a "black box" scenario, I prefer the second also, for object-oriented persuasion.

I like option 1, so it can be used on any value that may be in cents, not just that specific field in that specific class. It leaves the option to be used elsewhere in your code without code duplication.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top