Polymorphism
“How to handle alternatives based on type?” – Craig Larman
When related alternatives or behaviors vary by type (class), assign responsibility for the behavior—using polymorphic operations—to the types for which the behavior varies.
When to use
When you see a switch or if statement checking the type of an object to decide what to do.
Why it matters
- Extensibility: You can add new types without modifying the main logic (OCP).
- Simplicity: Eliminates massive
switch/caseblocks.
Signs of Violation
-
switch (type) { case 'A': ... case 'B': ... } -
if (obj instanceof Dog) ... else if (obj instanceof Cat) ...
Explanation
Problem
Conditional logic based on type is fragile. If you add a new type, you have to find and update all those conditional blocks scattered across the system.
Solution
Use the language’s polymorphic features. Define a common interface and let each class implement its own version of the behavior.
Real world analogy
You don’t need to check “If it’s a Dog, say woof; if it’s a Cat, say meow”. You just say “Speak!”, and the animal does what is natural for its type.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- Strategy Pattern: A common way to implement polymorphism for algorithms.
- Factory Pattern: Often used to create the polymorphic objects so the client doesn’t need to know the concrete type.
Code example
Typescript
Bad (Violation)
Good (Adherence)
PHP
Bad (Violation)
Good (Adherence)