AlgoViz

Aggregation vs composition

medium 45 min

Both say that an object has another one.

Watch lesson4:41

Where it shows up

The same pattern appears across everyday software.

  • Qt

    A QObject with a parent is added to the parent's children and is deleted when the parent is. The documentation calls this object trees.

  • C++ unique_ptr

    The reference describes it as owning an object and disposing of it when the pointer goes out of scope.

  • Rust

    The Rust book states the rules: each value has an owner, only one at a time, and the value is dropped when the owner goes out of scope.

  • PostgreSQL

    The manual on foreign keys recommends CASCADE when rows cannot exist independently, and RESTRICT or NO ACTION for independent objects.

  • Django

    Every ForeignKey names `on_delete`: CASCADE, PROTECT, SET_NULL and others. The developer must choose.

  • Jakarta Persistence

    `orphanRemoval` removes a part that is taken out of the relation. The specification says it is meant for entities privately owned by their parent.

  • EF Core owned entities

    Owned entity types are essentially part of the owner and cannot exist without it, says the documentation.

  • Kubernetes

    Owner references tell the control plane which objects depend on others, and garbage collection can delete dependents with their owner.

  • Git

    A tree refers to blobs by their content hash. The same blob can be in many trees, so a tree does not own it. Unreachable objects are removed later.