Design Pattern to track partial results of a complex process
I'm facing a programming problem that I don't know how to solve in a
object oriented and flexible way. I have in mind some bad solutions, but
I'm searching for a good one. I develop in Java, so I prefer Java ideas,
but any object oriented idea is welcome.
I've been searching for ideas, design patterns, or some algorithm that can
help me, but I don't know which terminology or name give to my problem, so
I couldn't find any clue.
Problem:
Summary:
I need to track partial results of a process that makes different changes
to a collection of entities. I need this to report to the user, the detail
of each calculation "step" in a table report. And also I need to persist
this collection in a database.
Detail:
The software I maintain has an entity similar to this one: public class
Salary { private Date date; private BigDecimal amount; }
That is grouped in a Collection, like this:
List<Salary> teamSalaries;
This set of entities can be modified by a set of "tasks" depending on some
rules:
Apply a certain tax (from a set of differents taxes)
Calculate a Future value for the money amount (More Info)
Discount an amount of money to keep below a maximum
etc...
For example:
public void processTeamSalaries(List<Salary> teamSalaries) {
applyTax(teamSalaries, Taxes.TAX_TYPE_1);
(...)
getFutureValue(teamSalaries, someFutureDate);
(...)
restrictToAMaximum(teamSalaries, Maximum.MARRIED_MAXIMUM);
(...)
applyTax(teamSalaries, TAXES.TAX_TYPE_3);
(...)
}
public void applyTax(List<Salary> teamSalaries, Tax taxToApply) {
for(Salary aSalary : teamSalaries) {
aSalary.setAmount(calculateAmountWithTax(aSalary.getAmount(),
taxToApply);
}
}
(...)
What I need is to process this collection of salaries, making changes to
the amount of money but preserving all the intermediate "states" of the
money amount, to show it to the user in a table with columns like these:
Date | Original Amount | Amount after tax 1 | Future Amount | Amount after
restriction | Amount after tax 2
My Ideas:
Add an attribute for each "partial result" on the class Salary
public class Salary {
(...)
private BigDecimal originalAmount;
private BigDecimal amountAfterFirstTax;
private BigDecimal amountAfterMaximumRestriction;
(...)
}
Problems:
The "steps" aren't rigid, maybe tomorrow one "step" changes, a new one
appears, or the "meaning" of some step changes. In that case I will need
to refactor the code too much.
Some "steps" can be repeated, so, how can I tell to the "method" in which
attribute have to "set" the result of the calculation?
Add a HashMap to the Salary class where I can put the partial results, and
pass to the "step" method the "key" where it have to put the partial
result
public class Salary {
(...)
HashMap<String, BigDecimal> partialResults;
(...)
}
Problems:
In some place I need to populate the HashMap to a JPA entity to save it on
my Database
If another developer changes the name of the key (for whatever reason)
maybe the "populating" of the attributes gets broken
Final Note: There are other similar situations with other "similar"
entities in my application, so it would be great if we can find a general
solution for this :D
No comments:
Post a Comment