The Don't Repeat Yourself (DRY) Principle says that each piece of knowledge or behavior should have one authoritative representation in code. Duplicate code enlarges programs, complicates maintenance, and creates change leaks when one copy is missed or changed differently. section 2.3.3
Recognize DRY violations through concrete design symptoms:
- Repeated methods with nearly identical code multiply the changes required by new requirements. section 2.3.3
- Growing subclass families and runtime isinstance checks signal that behavior has been scattered across type-specific code. section 2.3.3
- Share repeated behavior through one separate function or one cohesive class, then use suitable design relationships rather than unnecessary subclasses. section 2.3.3 section 2.4
Toy behaviors before refactoring
ToyCar and TrainSet duplicate the same play implementation, while ToyCar and ModelAirplane duplicate the same sound implementation. Each subclass also hardcodes its play action and sound, so adding subclasses or changing behavior increases duplicated maintenance work. section 7.3.1









