Push Down Method
Behavior on a superclass is relevant only for some of its subclasses.
“Behavior on a superclass is relevant only for some of its subclasses.”
Move it to the subclass.
graph LR
A[Superclass::bark()] -->|Refactoring| B[Dog::bark()]
When to apply (Smells)
- Refused Bequest: Superclass has
bark()butCatinherits it and throws exception.
Motivation
- Class should only contain what it supports.
Mechanics (Steps)
- Move method to subclass.
- Remove from superclass.
Explanation
Problem
Animal has bark(). Cat shouldn’t bark().
Solution
Move bark() to Dog.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After