Skip to main content

Refactoring 023 - Replace Inheritance with Delegation

Transform your rigid inheritance into flexible delegations

Updated 3 min readMarkdown
Part of seriesRefactorings
Refactoring 023 - Replace Inheritance with Delegation

TL;DR: Replace restrictive inheritance hierarchies with flexible object delegation

Problems Addressed ๐Ÿคฏ

  • Liskov substitution violation
  • Rigid class hierarchy
  • Hidden dependencies
  • Tight Coupling
  • Limited Reusability
  • Single Responsibility principle violation

Related Code Smells ๐Ÿง‘โ€๐Ÿ’ป

Steps ๐Ÿ”„

  1. Create a temporary field in the subclass for the superclass.
  2. Update subclass methods to delegate calls.
  3. Add delegation methods for inherited behavior.
  4. Remove inheritance and update object creation.

Sample Code ๐Ÿ’ป

Before ๐Ÿšจ

class Chatbot {    
    void respond(String question) {
        // Here is the logic to answer a question
    }
}

class Robot extends Chatbot {
    // The Physical Robot inherits the logic
    // to answer questions
    // and adds physical behavior
    void move() {
        System.out.println("Moving...");
    }

    void grab() {
        System.out.println("Grabbing object...");
    }
}

After

class Brain {
    String answer(String question) {
        // The common logic to answer questions
        // is extracted into a different object
        return "Thinking... Answering: " + question;
    }
}

final class Chatbot {    
    private final Brain brain;

    Chatbot(Brain brain) {
        this.brain = brain;
    }

    void respond(String question) {
        System.out.println(this.brain.answer(question));
    }
}

final class Robot {
    // 4. Remove inheritance and update object creation.
    private final Brain brain;    
    // 1. Create a temporary field in the subclass for the superclass.
    // private final Chatbot chatbot;  

    Robot(Brain brain) {
        this.brain = brain;
        // 2. Update subclass methods to delegate calls.
        // this.chatbot = new Chatbot(brain);
        // This code is removed after step 4
    }

    void move() {
        System.out.println("Moving...");
    }

    void grab() {
        System.out.println("Grabbing object...");
    }

    void respond(String question) {
        // 3. Add delegation methods for inherited behavior.
        // chatbot.respond(question);
        // This code is also removed after step 4 
        System.out.println(this.brain.answer(question));
        // The physical robot can also use it as text-to-speech
    }
}

Type ๐Ÿ› ๏ธ

[X] Semi-Automatic

Safety ๐Ÿ›ก๏ธ

This refactoring is safe when done carefully and with proper testing.

You should ensure all delegated method signatures match exactly and maintain existing behavior.

The main risk comes from missing methods that need delegation or incorrectly implementing the delegation methods.

Why is the Code Better? โœจ

You gain the flexibility to change implementations at runtime and avoid the pitfalls of inheritance like tight coupling.

How Does it Improve the Bijection?

This refactoring improves the Bijection between code and reality by better modeling real-world relationships.

A robot doesn't inherit from a brain in the real world - it has a brain.

By replacing inheritance with delegation, you create a more accurate representation of the actual relationship between objects using the MAPPER.

Limitations โš ๏ธ

The rewriting requires writing additional delegation methods.

If subclass logic relies too much on the superclass, delegation might increase boilerplate.

Refactor with AI

Tags ๐Ÿท๏ธ

  • Inheritance

Related Refactorings ๐Ÿ”„

See also ๐Ÿ“š

Credits

Image by Gerd Altmann on Pixabay


This article is part of the Refactoring Series.

About the author

  • Iโ€™m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

No comments yet. Be the first to comment.