AlgoViz

Composite pattern

medium 45 min

Composite builds trees of parts and wholes in which one part and the whole tree have the same type.

Watch lesson4:40

Where it shows up

The same pattern appears across everyday software.

  • DOM nodes

    Elements, texts and the document itself are all `Node`. `textContent` of an element is the joined text of its descendants. The child methods are on Node, so calling `appendChild` on a text node throws: the transparent form.

  • Java AWT and Swing

    `Container` is a `Component` that can contain other AWT components. `add` exists on Container only: the safe form.

  • Python unittest

    A `TestSuite` is an aggregation of test cases and test suites. Running the suite is the same as iterating over it and running each test, and cases and suites share the methods run, countTestCases and debug.

  • Android ViewGroup

    A special view that can contain other views, called children. It is the base class of layouts.

  • three.js Object3D

    Base class of most objects in a scene. Objects can be added to objects, a child's position is relative to its parent, and `traverse` runs a function on an object and all descendants.

  • Django Q objects

    Conditions can be joined with the operators for and, or and not, and nested in parentheses. The result is again a Q object that `filter` accepts.

  • Elasticsearch bool query

    A query made of other queries in must, filter, should and must_not. Each clause may be a bool query again.

  • Godot nodes

    Everything in a scene is a Node. A node can have other nodes as children, and a tree of nodes is a scene. Freeing a node frees its children too.