Revision Notes/Design Patterns
Field Entry - Design Patterns
Composite Pattern
Composes objects into tree structures and lets you treat individual objects and groups of objects uniformly through the same interface.
Field Note - Real-world Analogy
“A company org chart: you can ask any node "how many people are under you?" A leaf node (individual employee) returns 1. A manager returns 1 + the count of everyone under their reports. A division head returns 1 + every manager's count. Same question, same interface, but handled recursively by the composite structure.”
Field Observations - Key Topics Covered
The pattern models a part-whole hierarchy. Some things are individual (a file), and some are containers that can hold both individual items and other containers (a folder). The Composite pattern lets you treat files and folders the same way — you can call size() on both, and the folder recursively sums up all its children.
4 more observations inside
Sign in to read every key point in full
Ready to go deeper?
Open the complete revision note.
Sign in to read every field observation in full, including a runnable code example and side-by-side comparisons with related patterns.
Read Full NotesSee Also in Design Patterns