liveBook · Manning

Favor composition over inheritance principle

As explained in Software Design for Python Programmers

The Favor Composition over Inheritance Principle recommends using composition instead of inheritance when composition provides a more flexible and maintainable design.

Favor composition when a has-a relationship makes behavior more flexible than an is-a hierarchy. Composed objects avoid hardcoding behavior, reduce duplication, and remain loosely coupled. section 7.3.3

Toy behaviors

A Toy receives PlayAction and Sound objects through its constructor. ToyCar, ModelAirplane, and TrainSet can therefore share or vary behaviors, and a train can change its sound at runtime. section 7.3.3

Composition provides:

  • Flexibility: assign or modify behaviors at runtime. section 7.3.3
  • Code sharing and delegation without duplicating behavior across subclasses. section 7.3.3
  • Loose coupling, encapsulation, simpler hierarchies, and no need for multiple inheritance. section 7.3.3
Listing 7.21 (Program 7.10 Toys-3): playaction.py (has-a relationships)
from abc import ABC, abstractmethod

class PlayAction(ABC):

    @staticmethod
    @abstractmethod
    def play(self): pass

class RollPlay(PlayAction):

    @staticmethod
    def play(): return 'roll it'

class FlyPlay(PlayAction):

    @staticmethod
    def play(): return 'fly it'

Shows interchangeable PlayAction implementations used as composed behaviors. section 7.3.3

How Software Design for Python Programmers covers it

How deep it goes
Demonstrates composition through concrete Python classes, delegation, constructor injection, setters, and runtime behavior changes.
Examples
Uses toys with interchangeable play and sound behaviors, including a train whose sound changes after creation.
What you can do after
Apply it by composing Python objects whose behaviors can be assigned, shared, delegated, and modified at runtime.
How it connects
Connects composition with inheritance, delegation, aggregation, loose coupling, encapsulation, and the Liskov Substitution Principle.

Also explained in

Compare how 2 books explain favor composition over inheritance principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.