Part 1 of 6 · 1 min read
Why this page comes ninth
In this module you will
- State the Composite pattern and say when data is a part-whole tree.
- Write a file and folder tree where a folder and a file share one interface.
- State the Iterator pattern and write an iterator over a tree.
- Say why Java's for-each loop works with your own classes.
- Name the costs of both patterns.
Many LLD problems hold a tree: a file system, a menu, an organisation chart, a board of squares grouped into rows. Composite handles the shape of such a tree. Iterator handles walking it. They are often taught together because a composite needs a way to be traversed. The sources are "Design Patterns" (GoF) chapters 4 and 5 and "Effective Java" (EJ) Item 58. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.