Composite
Description
A composite structure is a tree in which leaves and composites share a single interface, so clients can operate on the structure without knowing whether they’re holding a single atomic item or an arbitrarily-deep nested container. The defining property is uniform treatment:render(node) works whether node is a paragraph or a section-containing-other-sections; size(file_or_dir) works whether you’re holding a 4-byte file or a directory containing thousands of files.
The shape is everywhere: file systems (files and directories share “path with metadata”), DOM trees (text nodes and element nodes share the “Node” interface), mathematical expressions (atoms and sub-expressions both honor “evaluate”), org charts (individual contributors and managers both honor “report-up-the-chain”), nested menus, scene graphs.
The discipline is in keeping the interface honest: every operation on the component must be sensible for both leaves and composites, even when leaves implement it trivially. The temptation to add composite-only methods to the component interface is the canonical leak.
Triggers
User-initiated: User describes a part-whole hierarchy, recursive nesting, or wants tools to “work the same way at any depth.” Vocabulary cues: “nested,” “tree,” “recursive,” “part-whole,” “any-depth,” “leaves and branches.” Agent-initiated: Agent notices that current code branches on “is this an atomic X or a container of Xs?” or has duplicated logic for the singular and plural cases. Candidate inference: “introduce a shared component interface; treat both cases uniformly.” Situation-shape signals: Files-and-directories, items-and-groups, atoms-and-molecules, nodes-and-subtrees. Operations that should recurse into the structure (count, propagate, render, traverse). Repeatedif isinstance(x, Leaf) branches.
Exclusions
- Leaves and composites genuinely behave differently — if no real operation makes sense for both, the uniformity is fake and the abstraction obscures rather than helps. Tagged unions / sum types are the cleaner shape.
- Operations require structural awareness — if the client needs to know depth, parent chain, or sibling positions, the “uniform treatment” promise gets violated repeatedly and the composite becomes harder to read than the explicit structure.
- Two-level structures only — for strictly two-level part-whole (group + members, never group-of-groups), composite is overkill; a simple iterable container is enough.
Structure
Relationships
- recursion — a composite is defined recursively (a node is a leaf or a collection of nodes); recursion is the self-similar definition a composite instantiates.
- uniformity-dividend — composite’s payoff IS the uniformity dividend at recursive depth; the cost of maintaining the shared interface stays flat, the payoff grows with tree depth.
- frame-story — frame-story is composite in the narrative domain; nested narratives honoring the “story” interface let the same reading tools work at every level.
- stack-layer — composite generalizes stack-layer from linear to tree-shaped; stack-layer is the chain instance, composite is the branching instance.
- container — composite is “container-of-container-of-…-of-leaf”; recursive containment is what composite contributes on top of container.
- load-bearing — the uniform interface IS load-bearing for composite; if a composite-only method leaks into the component interface, the uniformity dividend collapses and the structure degrades to “tagged union with extra steps.”
Examples
Organizational hierarchies · business
Organizational hierarchies · business
Mathematical expressions · mathematics
Mathematical expressions · mathematics
1, x, (x + 1), (x + (y * 2)) all honor “evaluate,” “differentiate,” “simplify.”AST nodes in compilers · computer-science
AST nodes in compilers · computer-science
DOM tree · computer-science
DOM tree · computer-science
TextNode, Element, DocumentFragment all honor the Node interface; CSS / JS query selectors traverse the composite.File-system designs (UNIX inode-and-directory model, 1971). · computer-science
File-system designs (UNIX inode-and-directory model, 1971). · computer-science
composite in systems software. It makes traversal recursive without special-casing leaves vs. composites: ls -R, find, du, and tar all work because the same node abstraction handles both. The pattern propagated to UNIX-likes (Linux, BSD, macOS), to URI/URL hierarchies, to JSON/XML document trees, and to filesystem-like interfaces over non-filesystem resources (/proc, /sys, Plan 9’s 9P).Inference: when designing a hierarchical structure, ask whether the leaf and composite types can share an interface. If yes, you get traversal, search, and bulk operations for free; if no, every consumer has to dispatch on type.Gamma, Helm, Johnson, Vlissides (1994), Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four). Structural pattern, ch. 4. · computer-science
Gamma, Helm, Johnson, Vlissides (1994), Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four). Structural pattern, ch. 4. · computer-science
GoF Composite · computer-science
GoF Composite · computer-science
Mathematical / linguistic compositionality — the principle that the meaning of a whole is determined by its parts and their arrangement (Frege) · linguistics
Mathematical / linguistic compositionality — the principle that the meaning of a whole is determined by its parts and their arrangement (Frege) · linguistics
Nested menus / accordion UIs · computer-science
Nested menus / accordion UIs · computer-science
Scene graphs in 3D rendering · computer-science
Scene graphs in 3D rendering · computer-science
transform/render/bounds.