The Composite pattern simplifies hierarchical data by giving individual objects (leaves) and groups of objects (composites) the same interface, so client code can operate on either without special-casing. This turns tree traversal and manipulation into a uniform, recursive operation.
Key Points: • A common Component interface defines the operations both leaves and composites support. • Composite nodes hold child Component references and implement operations by delegating to their children recursively. • Clients call the same method regardless of whether they're holding a single leaf or an entire subtree. • Adding, removing, or counting elements works the same way at any level of the hierarchy. • New node types can be added without touching existing client code, since they just implement the same interface.
Example: In a UI framework, a Panel (composite) and a Button (leaf) can both implement render(), so a container simply calls render() on each child without checking whether it's a single widget or a nested panel.
Interview Tip: A concise interview answer is:
"Composite works by giving leaves and composite nodes the same interface, so the client never has to check 'is this a single item or a group' — it just calls the same method and lets the recursion handle the rest. That makes operations like rendering or searching a tree trivially uniform across the whole hierarchy."