Collapse Hierarchy
A superclass and subclass are not very different.
“A superclass and subclass are not very different.”
Merge them.
graph LR
A[Superclass] -->|Refactoring| C[Combined Class]
B[Subclass] --> C
When to apply (Smells)
- Lazy Class: A subclass that adds no value.
Motivation
- Simplicity: Why have two classes when one will do?
Mechanics (Steps)
- Select which one to remove (usually subclass).
- Pull up everything.
- Remove subclass.
Explanation
Problem
Employee and Salesman. Salesman has nothing special anymore.
Solution
Move everything to Employee and delete Salesman.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After