以State/Strategy取代类型码:
做法:
//重构前:
public class Employee {
static final int ENGINEER = 0;
static final int SALEMAN = 1;
static final int MANAGER = 2;
private int _type;
public Employee(int type) {
_type = type;
}
private int _monthlySalary = 7000;
private int _commission = 10000;
private int _bonus = 20000;
int payAmount() {
switch (_type) {
case MANAGER:
return _monthlySalary;
case SALEMAN:
return _monthlySalary + _commission;
case MANAGER:
return _monthlySalary + _bonus;
default:
throw new RuntimeException("Incorrect Employee");
}
}
public static void main(String[] args) {
Employee emp = new Employee(ENGINEER);
int salary = emp.payAmount();
System.out.println(salary);
}
}
package 第八章;
//重构后
package 第八章;
public class Employee {
private EmployeeType _type;
int getType() {
return _type.getType();
}
void setType(int arg) {
_type = EmployeeType.newType(arg);
}
private int _monthlySalary = 7000;
private int _commission = 10000;
private int _bonus = 20000;
public int get_monthlySalary() {
return _monthlySalary;
}
public int get_commission() {
return _commission;
}
public int get_bonus() {
return _bonus;
}
public int getAmount() {
return _type.payAmount(this);
}
public static void main(String[] args) {
Employee emp = new Employee();
emp.setType(EmployeeType.MANAGER);
System.out.println(emp.getAmount());
}
}
}
package 第八章;
abstract class EmployeeType {
static final int ENGINEER = 0;
static final int SALEMAN = 1;
static final int MANAGER = 2;
abstract int getType();
static EmployeeType newType(int code) {
switch (code) {
case ENGINEER:
return new Engineer();
case SALEMAN:
return new Salemen();
case MANAGER:
return new Manager();
default:
throw new IllegalArgumentException("Incorrect Employee code");
}
}
int payAmount(Employee emp) {
switch (getType()) {
case MANAGER:
return emp.get_monthlySalary();
case SALEMAN:
return emp.get_monthlySalary() +emp.get_commission();
case ENGINEER:
return emp.get_monthlySalary() + emp.get_bonus();
default:
throw new RuntimeException("Incorrect Employee");
}
}
}
package 第八章;
public class Engineer extends EmployeeType{
int getType() {
// TODO Auto-generated method stub
return EmployeeType.ENGINEER;
}
package 第八章;
public class Manager extends EmployeeType {
@Override
int getType() {
// TODO Auto-generated method stub
return EmployeeType.MANAGER;
}
}
package 第八章;
public class Salemen extends EmployeeType {
@Override
int getType() {
// TODO Auto-generated method stub
return EmployeeType.SALEMAN;
}
}
Replace conditional with Polymorphism 以多态取代条件表达式
使用场景:你手上有个条件表达式,它根据对象的类型不同而选择不同的行为。
使用方法:将这个条件表达式的每个分支放进子类内的覆写函数中,然后将原始函数声明为抽象函数。
重构上述的EmployeeType.class中的getAmount()方法
public class Engineer extends EmployeeType{
int getType() {
// TODO Auto-generated method stub
return EmployeeType.ENGINEER;
}
//多态部分重构
int getAmount(Employee emp) {
// TODO Auto-generated method stub
return emp.get_monthlySalary();
}
}
public class Engineer extends EmployeeType{
@Override
int getType() {
// TODO Auto-generated method stub
return EmployeeType.ENGINEER;
}
//多态部分重构
@Override
int getAmount(Employee emp) {
// TODO Auto-generated method stub
return emp.get_monthlySalary();
}
}
public class Salesman extends EmployeeType {
@Override
int getType() {
// TODO Auto-generated method stub
return EmployeeType.SALEMAN;
}
//多态部分重构
@Override
int getAmount(Employee emp){
return emp.get_monthlySalary()+emp.get_commission();
}
}
abstract class EmployeeType {
static final int ENGINEER = 0;
static final int SALEMAN = 1;
static final int MANAGER = 2;
abstract int getType();
//重构 新增加抽象类
abstract int getAmount(Employee emp);
static EmployeeType newType(int code) {
switch (code) {
case ENGINEER:
return new Engineer();
case SALEMAN:
return new Salesman();
case MANAGER:
return new Manager();
default:
throw new IllegalArgumentException("Incorrect Employee code");
}
}
//最终这部分可以删除
/*int payAmount(Employee emp) {
switch (getType()) {
case MANAGER:
return emp.get_monthlySalary();
case SALEMAN:
return emp.get_monthlySalary() +emp.get_commission();
case ENGINEER:
return emp.get_monthlySalary() + emp.get_bonus();
default:
throw new RuntimeException("Incorrect Employee");
}
}*/
}