Module 23 Β· Design Patterns (All 23 GoF) β Lesson 8 of 23 Β· ~6 min
Composite
Composite: treat parts and wholes uniformly
Problem: you have a tree β items inside groups inside groups β and you want one operation to work on a leaf and a branch the same way.
PawWalk scenario: a walk package made of individual items and sub-packages. total_price should work whether you call it on one item or the whole nested bundle.
The trick: both a leaf and a group answer the same method. A group's answer sums its children (which may themselves be groups).