AlgoViz

Object composition

medium 50 min

Build a big object from small ones.

Watch lesson4:23

Where it shows up

The same pattern appears across everyday software.

  • React

    Its earlier documentation recommends composition instead of inheritance to reuse code between components, and says that Facebook found no case for component inheritance in thousands of components.

  • Unity

    A GameObject is a container. The manual calls components the functional pieces of every GameObject.

  • Bevy

    Entities are unique things that get groups of components. Systems run over all entities with certain components.

  • Go embedding

    Effective Go says that the language has no typical type-driven subclassing, and shows how a struct borrows pieces of another by embedding it.

  • Kotlin by

    A class can implement an interface by delegating to an object. The documentation calls delegation a good alternative to implementation inheritance.

  • Java streams

    `new BufferedReader(new FileReader(name))`: buffering is an object around another, not a subclass per kind of file.

  • Guava Forwarding classes

    Base classes such as ForwardingList hold a delegate and pass every method on, so that you write only what you change.

  • Python logging

    A logger has handlers, a handler has a formatter and filters. Kinds of output and kinds of text vary on their own.

  • .NET HttpClient

    Delegating handlers are linked into a pipeline around outgoing requests: caching, errors, logging.

  • Kubernetes Pod

    A Pod composes containers that share storage and network, such as a web server and a sidecar that refreshes its files.