条件表达式,根据对象类型的不同而选择不同的行为。
将这个条件表达式的每一个分支放进一个子类内的覆写函数中,然后将原始函数声明为抽象函数。
动机:
多态使你不必编写明显的条件表达式。
如果同一组条件表达式在程序许多地方出现,那么使用多态的收益是最大的。
做法:
使用replace conditional with polymorphism之前,首先必须有一个继承结构。
如果要处理的条件表达式是一个更大函数中的一部分,首先对条件表达式进行分析,然后使用extract method。
如果有必要,使用move method将条件表达式放置到继承结构的顶端。
任选一个子类,在其中建立一个函数,使之覆写超类中容纳条件表达式的那个函数。将与该子类相关的条件表达式分支复制到新建函数中,并调整。
旧代码
class Engineer extends EmployeeType{
int getTypeCode(){
return Employee.ENGINEER;
}
}
class Manager extends EmployeeType{
int getTypeCode(){
return Employee.MANAGER;
}
}
class Salesman extends EmployeeType{
int getTypeCode(){
return Employee.SALEMAN;
}
}
class Employee...
private employeeType _type;
int payAmount(){
switch(getType()){
case EmployeeType.ENGINEER:
return _monthlySalary;
case EmployeeType.SALESMAN:
return _monthlySalary + _commission;
case EmployeeType.MANAGER:
return _monthlySalary + bonus;
default;
throw new RuntimeException();
}
}
int getType(){
return _type.getTypeCode();
}
void setType(int arg){
_type = EmployeeType.newType(arg);
}
class employeeType...
static EmployeeType newType(int code){
switch(code){
case ENGINEER:
return new Engineer();
case SALESMAN:
return new Salesman();
case MANAGER:
return new Manager();
default:
throw new IllegalArgumentException();
}
}
static final int ENGINEER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
新代码
class Employee...
private Engineer _type;
int payAmount(){
return _type.payAmount(this);
}
class Engineer...
int payAmount(Employee emp){
return emp.getMonthlySalary();
}
class Salesman...
int payAmount(Employee emp){
return emp.getMonthlySalary() + emp.getCommission();
}
class Manager...
int payAmount(Employee emp){
return emp.getMonthlySalary() + emp.getBonus();
}
class EmployeeType...
abstract int payAmount(Employee emp);