9.6 replace conditional with polymorphism (以多态取代条件表达式)

条件表达式,根据对象类型的不同而选择不同的行为。

将这个条件表达式的每一个分支放进一个子类内的覆写函数中,然后将原始函数声明为抽象函数。

动机:

多态使你不必编写明显的条件表达式。

如果同一组条件表达式在程序许多地方出现,那么使用多态的收益是最大的。

做法:

使用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);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值