Composition

  • Definition: Composition is a strong association where the child cannot exist independently of the parent.
  • Ownership: The parent object is responsible for the lifecycle of its child objects. The child objects do not exist without the parent.
  • Lifecycle: If the parent object is destroyed, the child objects are also destroyed.

Example: A House contains Rooms. If the House is destroyed, the Rooms are also destroyed.

classDiagram
    class House {
        +Room[] rooms
    }
    class Room {
    }

    House "1" *-- "0..*" Room : Composition

In Mermaid Composition:

  • Represented by a filled diamond (*--).
  • Indicates a “part-of” relationship where the parent strictly owns the child, and the child cannot exist without the parent.