반응형
before
public class ReplaceParameterWithExplicitMethod_before {
static final int ENGINEER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
static public class Employee {
// ...
private int type;
private Employee(int type) {
this.type = type;
}
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");
}
}
public String getName() {
String name = "None";
switch(type) {
case ENGINEER:
name = "ENGINEER"; break;
case SALESMAN:
name = "SALESMAN"; break;
case MANAGER:
name = "MANAGER"; break;
}
return name;
}
}
static class Engineer extends Employee {
public Engineer() {
super(ENGINEER);
}
}
static class Salesman extends Employee {
public Salesman() {
super(SALESMAN);
}
}
static class Manager extends Employee {
public Manager() {
super(MANAGER);
}
}
public static void main(String[] args) {
Employee kent = Employee.create(ENGINEER);
System.out.println(kent.getName());
}
}
after
public class ReplaceParameterWithExplicitMethod_before {
static final int ENGINEER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
static public class Employee {
// ...
private int type;
private Employee(int type) {
this.type = type;
}
static Employee create(int type) {
switch (type) {
case ENGINEER:
return createEngineer();
case SALESMAN:
return createSalesman();
case MANAGER:
return createManager();
default:
throw new IllegalArgumentException("Incorrect type code value");
}
}
public static Engineer createEngineer() {
return new Engineer();
}
public static Salesman createSalesman() {
return new Salesman();
}
public static Manager createManager() {
return new Manager();
}
public String getName() {
String name = "None";
switch(type) {
case ENGINEER:
name = "ENGINEER"; break;
case SALESMAN:
name = "SALESMAN"; break;
case MANAGER:
name = "MANAGER"; break;
}
return name;
}
}
static class Engineer extends Employee {
public Engineer() {
super(ENGINEER);
}
}
static class Salesman extends Employee {
public Salesman() {
super(SALESMAN);
}
}
static class Manager extends Employee {
public Manager() {
super(MANAGER);
}
}
public static void main(String[] args) {
Employee kent = Employee.createEngineer(); //이거 이렇게 할 수 있다
System.out.println(kent.getName());
}
}
반응형
'Design Architecture > Refactoring' 카테고리의 다른 글
Introduce Parameter Object (0) | 2022.04.20 |
---|---|
Replace Parameter with Method Call (0) | 2022.04.20 |
Separate Query From Modifier (0) | 2022.04.20 |
Replace Nested Conditional With Guard Clauses (0) | 2022.04.20 |
Decompose Conditional (0) | 2022.04.20 |