EMZETT.
Login

Composite pattern

In short: The composite pattern treats single objects and groups of objects the same. That way tree structures can be handled uniformly.

In more detail: A common interface applies to “leaves” (single elements) and “containers” (which hold further elements). A call such as size() on a folder recursively adds the sizes of all contents.

In Depth

  • Examples: file system (file and folder), graphics programs (shape and group), UI trees (components inside components), HTML elements.
  • Advantage: Callers need not distinguish between a single part and a group.
  • Disadvantage: The interface can become too general.
  • Related: tree data structure, recursion.

See also: Decorator pattern, Facade pattern