What is the Composite pattern, how is it used for tree structures, and what are its traps?
Short answer
Composite lets you treat a single item and a group the same way: both implement the same interface, and a group calls each child. It fits anything shaped like a tree.
3 points to say:
- Leaves and groups implement one interface.
- Callers need no
instanceof. - Watch out for very deep trees and cycles.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.