35

Replace Type Code with Subclasses


Goal

A 'kind' string field becomes a real subclass type; the type system enforces the legal set.

Before the refactoring
class Employee {
  type; // 'engineer' | 'manager'
}
After the refactoring
class Employee {}
class Engineer extends Employee {}
class Manager  extends Employee {}
Savings

Compile-time checks that no kind is missed; per-kind behavior lives where it belongs.

Note

If only one or two switches exist on the type code, subclassing is over-design; combine with Replace Conditional with Polymorphism only when dispatch repeats.