Grokking Design Patterns for Engineers and Managers
0% completed
Composite Pattern
The Composite Pattern lets a single object and a whole tree of objects be used through the same interface.
Some structures nest. A folder holds files and other folders. An invoice holds line items and bundles that hold their own items. A UI panel holds widgets and other panels.
Now compute the total size, or the total price, or draw the whole thing. Without a pattern, every one of those operations starts the same way:
if (node instanceof Folder) { int total = 0; for (Node child : ((Folder) node).children()) total += sizeOf(child); return total;
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%