Composite Pattern

noun

say it: “kum-PAH-zit PAT-urn” /kəmˈpɑzɪt ˈpætərn/

A design pattern where a group answers the same questions as a single item, by asking each of its parts.

What is the Composite pattern?

The Composite pattern lets a group of things answer the same questions as a single thing, so code treats one item and a whole group alike. A box and a pallet both answer weight(): a box with its own pounds, a pallet by asking each thing on it, other pallets included, however deep they're stacked. No code has to ask "is it a box or a pallet?".

In a sentence

  • “With the Composite pattern, a pallet weighs itself by asking each thing on it, so stacking pallets ten deep needs no new code.”

Level: Journeyman · More OOP terms at this level

Updated