Reputation: 1384
I want to develop a system that is similar to calculation of salary. A salary has a basic value. On top of it, an employee can get some Bonus (es) or Penalties. Decorator Pattern seems to fit this scenario
Salary finalSalary = new HardworkBonus(new LateComingPenalty(new BasicSalary()))
System.out.println("Your total salary is : "+ finalSalary.calculate())
In addition, I want to save the result of each computation. So in the end, even after calculation, I can retrieve how much was the LateComingPenalty.
It seems to be a common problem where such sort of invoice calculations is involved.There might be some better options than Decorator Pattern.Do you have some better suggestions?
Upvotes: 2
Views: 1106
Reputation: 994897
That seems like a bit of overengineering. I might suggest:
class Salary {
double base;
SalaryAdjustment[] adjustments;
double getSalary() {
double r = base;
for (SalaryAdjustment a: adjustments) {
r += a.getAdjustment();
}
return r;
}
};
In adjustments
you can add your HardworkBonus
and LatePenalty
or whatever else, and retrieve them later.
Upvotes: 7