Replace Conditional with Polymorphism

You have a conditional that chooses different behavior depending on the type of an object.

Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.

double getSpeed() {
  switch (_type) {
    case EUROPEAN:
      return getBaseSpeed();
    case AFRICAN:
      return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
      return (_isNailed) ? 0 : getBaseSpeed(_voltage);
  throw new RuntimeException ("Should be unreachable");


image/svg+xml Norwegian Blue getSpeed African getSpeed European getSpeed Bird getSpeed