vendredi 28 avril 2017

Is there a better way of not repeating code than Template Method Design Pattern?

There are Employees to model, and the problem to solve is to calculate the salary. There are diferent types of employees, all of them share discount for Social Work:

The Generic Employee:

public class Employee{

    float salary;

    public Empleado(float salary) {
        this.salary = salary;
    }

    public float salary() {
        return this.minusSocialWork(this.salary);
    }

    private float descuentoObraSocial(float sueldo) {
        return (float) (salary * 0.87);
    }

}

And there are subtypes:

  • Employee with family and basic salary (has a Bono Plus if has childrens + basic salary)
  • Plant Employee (for each children (son, daughter) $150 + basic salary)
  • Trainee Employee (salary is hours * price by hour)
  • Temporary employee (salary is hours * price by hour + Bono Plus + basic salary)

Question:

Using Template Method Design Pattern, I find a lot of duplicate code, so my question is: is there a better close up when there are so many subtypes or kinds of Employees and combinations?

Aucun commentaire:

Enregistrer un commentaire