Pull Up Field
Two subclasses have the same field.
“Two subclasses have the same field.”
Move it to the superclass.
graph LR
A[Subclass A::field] -->|Refactoring| B[Superclass::field]
C[Subclass B::field] --> B
When to apply (Smells)
- Duplication: Same field in methods of sibling classes.
Motivation
- DRY: Eliminate duplicate fields.
Mechanics (Steps)
- Inspect candidate fields (same name, same type).
- Create field in superclass.
- Remove fields from subclasses.
Explanation
Problem
Subclasses handle the same data.
Solution
Move the data to the superclass.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After