mercredi 11 mai 2016

How to model classes that do calculations and store them?

I have to develop a class, part of a financial application, which receives two properties and returns two results. Before you think that it is not a class, but method(s), I have to say that I have to persist both: the two user-provided parameters and the two outputs. Let's illustrate like follows in this mock:

 ----------------
|PetWash         |
|----------------|
|petWeight       |<- user provided
|petHeight       |<- user provided
|ammountSoapUsed |<- system calculated
|price           |<- system calculated
 ----------------

  1. Should I do calculations in model classes? eg., the same model class that represents this entity should enclose the methods that do these calculations? Or should I create a kind of "calculation Engine" that would return data and store it in calculated fields?

  2. If the first case, should I invoke calculations in the getter methods or just create a "calculate" method which would update the value for ammountSoapUsed and price? In this sense, should I just store petWeight and petHeight and calculate ammountSoapUsed and price everytime that they are needed (remember that in the real-life case calculation is much more complex)?

In truth, I'm not interested in what I could do, but in what OOP best practices recommend to do. Can you help me?

Aucun commentaire:

Enregistrer un commentaire