Skip to content

Instantly share code, notes, and snippets.

@up1
Last active January 6, 2016 02:42
Show Gist options
  • Save up1/b84d756ac75738677dac to your computer and use it in GitHub Desktop.
Save up1/b84d756ac75738677dac to your computer and use it in GitHub Desktop.
When to refactoring
public class BonusCalculator {
public double getBonus(Employee employee) {
if( employee.department == Department.IT ) {
if(employee.year >= 5) {
return 0.5 * employee.salary;
}
return 0;
} else {
return 1000 + 500 * employee.salary;
}
}
}
public class BonusCalculator {
public double getBonus(Employee employee) {
switch (employee.department) {
case IT:
if (employee.year >= 5) {
return 0.5 * employee.salary;
}
return 0;
case Sales:
return 1000 + 500 * employee.salary;
default:
return 0;
}
}
}
public class BonusCalculator {
public double getBonus(Employee employee) {
switch (employee.department) {
case IT:
if (employee.year >= 5) {
return 0.5 * employee.salary;
}
return 0;
case Sales:
return 1000 + 500 * employee.salary;
case Production:
return 0;
case Accounting:
return 0;
case Legal:
return 0;
case HR:
return 0;
default:
return 0;
}
}
}
interface CalculationStrategy {
double calculate(Employee employee);
}
class ITBonusCalculation implements CalculationStrategy {
@Override
public double calculate(Employee employee) {
return 0;
}
}
class SalesBonusCalculationStrategy implements CalculationStrategy {
@Override
public double calculate(Employee employee) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment