Skip to main content

Composite Pattern

Learn Composite Pattern (Structural Patterns, Design Patterns in Java) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

The Composite Pattern lets you treat individual objects and groups of objects through the same interface , so client code doesn't need to know whether it's dealing with a single item or a whole tree of them. Real-Life Example: A file system - a File and a Folder (which can contain Files and other Folders) both expose the same "get size" operation An org chart - an individual employee and an entire department both respond to "list all members" A UI toolkit - a single Button and a Panel full of widgets both respond to "render" Why do we need Composite? Represent part-whole (tree) hierarchies...

More lessons in Structural Patterns