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() but Cat inherits it and throws exception.

Motivation

  • Class should only contain what it supports.

Mechanics (Steps)

  1. Move method to subclass.
  2. Remove from superclass.

Explanation

Problem

Animal has bark(). Cat shouldn’t bark().

Solution

Move bark() to Dog.

Pros and Cons

Pros Cons
  • Clarity
  • None
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After