dimanche 27 décembre 2020

Java Object Fields Value Depend on Other Fields - Best Practice Patterns?

I have a simple java object

 public class Order {
  private int quantity;
  private long price;
  private long totalPrice;
 }

I will only be storing quantity and price, and totalPrice field will be generated based on quantity * price.

Now i have method called populateTotalPrice() in Order class. But it seems that i am putting a logic inside an entity class which may be an anti pattern?

My other option, is to use a helper static method.

What is really the best practice for this type of behaviour?

Aucun commentaire:

Enregistrer un commentaire