Repaso
Español
Design & PracticesIntermediateDP-49

Which pattern lets you treat a single object and a group of objects in a uniform way (a tree, like folders containing files and folders)?

Options

  1. Composite (Correct option)
  2. Command
  3. Singleton
  4. Observer

Answer

Composite

Why

Composite: both a leaf (file) and a container (folder) implement the same interface, so client code can call size() on either one. A container calls the same method on all its children.

Examples: file system, UI component trees (a panel containing buttons and panels), organization charts, menus.

Remember

Composite = tree where leaf and group share one interface.

Key terms

Practice this question in Repaso