Push Down Field
A field is used only by some subclasses.
“A field is used only by some subclasses.”
Move it to the subclass.
graph LR
A[Superclass::field] -->|Refactoring| B[Subclass A::field]
When to apply (Smells)
- Refused Bequest: Superclass has a field
quotabut not all employees have quotas.
Motivation
- Clarity: Only classes that need the data should have it.
Mechanics (Steps)
- Declare field in subclasses.
- Remove from superclass.
Explanation
Problem
Employee has quota, but Engineer doesn’t use quota.
Solution
Move quota to Salesman.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After