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
- Composite (Correct option)
- Command
- Singleton
- 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.