有一个类型码,它会影响类的行为,但你无法通过继承消除它
public class Employee {
static final int ENGINNER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
private int type;
// 月薪.
private int montylySalary;
// 佣金.
private int commission;
// 奖金.
private int bonus;
public Employee(int type) {
this.type = type;
}
public int payAmount() {
switch(getType()) {
case ENGINNER: return montylySalary;
case SALESMAN: return montylySalary + commission;
case MANAGER: return montylySalary + bonus;
default:
throw new IllegalArgumentException("Incorrect Employee.");
}
}
public int getType() {
return type;
}
}
重构:以状态对象取代类型码
public abstract class EmpoyeeType {
static final int ENGINNER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
public abstract int getTypeCode();
public static EmpoyeeType newType(int code) {
switch (code) {
case ENGINNER: return new Engineer();
case SALESMAN: return new Salesman();
case MANAGER: return new Manager();
default:
throw new IllegalArgumentException("Incorrect Employee Code.");
}
}
}
public class Engineer extends EmpoyeeType {
public int getTypeCode() {
return Employee.ENGINNER;
}
}
public class Manager extends EmpoyeeType {
public int getTypeCode() {
return Employee.MANAGER;
}
}
public class Salesman extends EmpoyeeType {
public int getTypeCode() {
return Employee.SALESMAN;
}
}
public class Employee {
private EmpoyeeType type;
// 月薪.
private int montylySalary;
// 佣金.
private int commission;
// 奖金.
private int bonus;
public Employee(int type) {
this.type = type;
}
public int payAmount() {
switch(getType()) {
case EmpoyeeType.ENGINNER: return montylySalary;
case EmpoyeeType.SALESMAN: return montylySalary + commission;
case EmpoyeeType.MANAGER: return montylySalary + bonus;
default:
throw new RuntimeException("Incorrect Employee.");
}
}
public EmpoyeeType getType() {
return type;
}
}
再继续Replace Conditional with Polymorphism(以多态取代条件表达式)
public abstract class EmpoyeeType {
static final int ENGINNER = 0;
static final int SALESMAN = 1;
static final int MANAGER = 2;
public abstract int getTypeCode();
public abstract int payAmount(Employee employee);
public static EmpoyeeType newType(int code) {
switch (code) {
case ENGINNER: return new Engineer();
case SALESMAN: return new Salesman();
case MANAGER: return new Manager();
default:
throw new IllegalArgumentException("Incorrect Employee Code.");
}
}
}
public class Engineer extends EmpoyeeType {
public int getTypeCode() {
return Employee.ENGINNER;
}
public int payAmount(Employee employee) {
return employee.getMontylySalary();
}
}
public class Manager extends EmpoyeeType {
public int getTypeCode() {
return Employee.MANAGER;
}
public int payAmount(Employee employee) {
return employee.getMontylySalary() + employee.getBonus();
}
}
public class Salesman extends EmpoyeeType {
public int getTypeCode() {
return Employee.SALESMAN;
}
public int payAmount(Employee employee) {
return employee.getMontylySalary() + employee.getCommission();
}
}
public class Employee {
private EmpoyeeType type;
// 月薪.
private int montylySalary;
// 佣金.
private int commission;
// 奖金.
private int bonus;
public Employee(int type) {
this.type = type;
}
public int payAmount() {
return getType().payAmount(this);
}
public EmpoyeeType getType() {
return type;
}
}