I am reviewing areas where I can optimize the design for my mortgage calculation tool, mostly for learning purposes. After reading about Anemic Domain Models, I became interested in creating Rich Models, and noticed that my current implementation might have anemia! Here is the current implementation in pseudocode:
class MortgageCalculator {
Mortgage mortgage; // mortgage object containing loanAmount, interest rate, etc.;
calculateMonthlyPayment(); // calculates monthly payments using mortgage object's properties
}
class Mortgage { // Anemic?
loanAmount;
interestRate;
}
Currently the Mortgage object serves primarily for data-transfer between objects, etc.
Here are some revision options I am considering:
- Remove Mortgage object out of the MortgageCalculator and use Mortgage purely as a DTO while the calculator's methods take arguments (e.g, calculateMonthlyPayment(loanAmount,interestRate). This would help with decoupling MortgageCalculator from the Mortgage object, but would still have Mortgage as an anemic model.
- Merge both classes into one "rich" MortgageCalculator model, which contains both the business logic (e.g, calculateMonthlyPayment) and mortgage properties (e.g, loanAmount). My concern here is that I am not sure if it is necessary for a calculator object to hold its operands as instance variables, but they would facilitate data transfer, storage and perhaps resolve the anemia?
I am wondering what the ideal approach would be, or if I am missing the point?