开发者

Salary Calculations Patterns

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 calculati开发者_开发技巧ons is involved.There might be some better options than Decorator Pattern.Do you have some better suggestions?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜