Software engineering / Composite pattern

Composite pattern

Files and folders form a tree. We give both the same operation, size(), and watch one call on the top folder work its way down the tree and add itself back up.

Intermediate7 steps~6 min
File (leaf) — a single item with no children
Folder (composite) — a group that holds other entries
Answer — a size handed back up the tree
// THE SHORT VERSION

In 30 seconds

The composite pattern lets code treat a single item and a group of items the same way. A file and a folder both answer the question “how big are you?”.

A file answers with its own size. A folder answers by asking everything inside it and adding up the replies. Since a folder can contain folders, the question travels down the whole tree by itself: that is recursion.

The caller never needs to know whether it holds one item or a tree of thousands. File systems and user interfaces are built like this.

Composite pattern, step by step

STEPS01 / 07

Left out: operations that only make sense for groups, such as add(child). Designs differ on whether single items should offer them too, trading safety against uniformity.

STEP 01 / 07 Single items and groups
THE MATH
file: holds data folder: holds files and folders
  1. Step 1: Single items and groups. A file system has two kinds of thing. A file is a single item with a size of its own. A folder is a group: it holds files, and it can hold other folders, which hold more. The result is a tree. Here the folder project contains two files and a folder, src, with two more files. How big is project?
  2. Step 2: The awkward way. Without a pattern, the code asking the question must treat the two kinds differently: read the size of a file, but loop through a folder. And since a folder may contain folders, each level of depth needs another loop inside the last. The caller ends up knowing both kinds and guessing how deep the tree goes.
  3. Step 3: One shared interface. The composite pattern starts by giving both kinds the same interface. Call it Entry: anything that can answer size(). A File is an Entry. A Folder is an Entry too, and it holds a list of entries without caring whether each is a file or a folder. In the pattern’s terms, File is a leaf and Folder is a composite.
  4. Step 4: A folder asks its children. Now each kind answers size() in its own way. A file returns its own number. A folder has no size of its own: it asks every child for its size and adds up the answers. For src that is 6 + 2 = 8 KB. Notice that the folder calls size() on each child without checking what kind of entry it is.
  5. Step 5: Recursion down the tree. Ask project for its size. It asks its three children. The two files answer 4 and 12 at once. The third child, src, is a folder, so it runs the same method: it asks its own children, gets 6 and 2, and answers 8. Project adds 4 + 12 + 8 = 24 KB. An operation that calls itself on smaller parts like this is recursive.
  6. Step 6: The caller cannot tell. This is the point of the pattern. The calling code holds some Entry and writes entry.size(). The same line works whether the entry is a single file, a folder, or a whole tree. A group can be used anywhere a single item can, so trees of any depth need no special handling.
  7. Step 7: Where you meet it. User interfaces are built the same way. A window contains panels, panels contain buttons and text, and telling the window to render reaches every element by the same recursion. Web pages (the DOM), grouped shapes in drawing programs and file systems share this shape: a tree in which groups and single items share one interface.
VALUES SO FAR
files4, 12, 6, 2 KBleaves
foldersproject, srcgroups
shared····interface
src····sum
project····result
caller····uniform
also····examples

Spotted a mistake on this page?