반응형
TODO
before
public class ReplaceTypeCodeWithSubclasses_before {
static class Employee {
//...
static final int ENGINEER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
private int type;
public Employee (int arg) { type = arg; }
public int monthlySalary;
public int commission;
public int bonus;
public int payAmount() {
switch (type) {
case ENGINEER : return monthlySalary;
case SALESMAN : return monthlySalary + commission;
case MANAGER : return monthlySalary + bonus;
default : throw new RuntimeException("Incorrect Code");
}
}
public void initializeBasicSalary(int monthlySalary, int commission, int bonus) {
this.monthlySalary = monthlySalary;
this.commission = commission;
this.bonus = bonus;
}
}
public static void main(String[] args) {
Employee employee = new Employee(Employee.ENGINEER);
employee.initializeBasicSalary(450, 80, 100);
System.out.println("pays : " + employee.payAmount());
}
}
after
abstract int getType();
static Employee create(int type) {
switch (type) {
case ENGINEER:
return new Engineer();
case SALESMAN:
return new Salesman();
case MANAGER:
return new Manager();
default:
throw new IllegalArgumentException("Incorrect type code value");
}
}
반응형
'Design Architecture > Refactoring' 카테고리의 다른 글
Replace Nested Conditional With Guard Clauses (0) | 2022.04.20 |
---|---|
Decompose Conditional (0) | 2022.04.20 |
Replace type Code with Class (0) | 2022.04.20 |
Encapsulate Collection (0) | 2022.04.20 |
Replace Data Value with Object (0) | 2022.04.20 |