gof-composite
Use when treating individual objects and groups of objects uniformly — phrases like "Composite pattern", "tree structure", "menus and submenus both behave like menu items", "I want to call `display()` on either a leaf or a branch and have it work", "part-whole hierarchy". Defines Composite as composing objects into tree structures so clients can treat individual leaves and composite branches uniformly through a shared interface.
How do I install this agent skill?
npx skills add https://github.com/yonatankarp/software-design-skills --skill gof-compositeIs this agent skill safe to install?
- Gen Agent Trust Hubpass
The skill provides educational content and documentation regarding the Composite software design pattern and contains no executable code or security risks.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
What does this agent skill do?
One-line summary
Compose objects into tree structures to represent part-whole hierarchies, letting clients treat individual objects (leaves) and compositions of objects (branches) uniformly.
When to use this skill
- A part-whole hierarchy: file system (files and directories), menus and submenus, GUI components (button vs panel of components), organizational hierarchies.
- Client code needs to call the same operations on both leaves and branches and not care which it has.
- Recursive structures where the recursion shouldn't show in the calling code.
When NOT to use this skill
- A flat collection — there's no hierarchy, just use a list.
- The operations are genuinely different for leaves vs. branches and forcing them through one interface awkwardly buries the distinction.
- Two-level structures with only one layer of grouping — sometimes a
List<Item>is enough without inventing a Composite.
Core content
The pattern introduces a common interface (Component) shared by leaves and composites. Both implement the operations the client cares about; composites implement them by delegating to their children.
Component { operation() }
Leaf { operation() /* does the work */ }
Composite { children: List<Component>
operation() { children.forEach { it.operation() } }
add(c); remove(c); }
Trade-off about transparency vs safety:
- Transparent Composite —
Componentdeclaresadd(child). Both leaves and composites have the method. Clients can treat them uniformly butLeaf.add(child)either no-ops or throws. - Safe Composite —
add(child)lives only onComposite. Type-safe but clients must check the type before adding.
HFDP favors transparent in its examples; in practice, safe is often cleaner unless uniform handling really matters.
Decision heuristics
- Composite is worth its weight when all the operations meaningfully apply to both leaves and branches.
- If only one operation applies uniformly and the rest differ, the abstraction is forced — consider whether you need Composite at all.
- For very deep or very wide trees, watch out for stack-recursive operations exploding the stack — flatten with an Iterator if needed.
Anti-patterns
- Composite with operations that only make sense on leaves, no-oped on composites (or vice versa) — uniformity is performative, not real.
- "Composite" that's really a list — no recursion, no branches with children. It's just a collection.
- Composite that exposes its children directly via
getChildren()and lets callers mutate the tree externally — destroys invariants.
See also
gof-iterator— the natural way to walk a Composite tree.gof-decorator— same structural shape (both wrap), different intent (Decorator adds behavior; Composite represents part-whole).gof-kotlin-idioms— sealed class hierarchies (sealed interface Component { class Leaf; class Branch(val children: List<Component>) }) plus exhaustivewhenmake Composite explicit and type-safe.
References
- Head First Design Patterns (2nd ed), Ch. 9 ("Well-Managed Collections: the Iterator and Composite Patterns") — Composite is illustrated with menus-of-menus.
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/yonatankarp/software-design-skills/gof-composite">View gof-composite on skillZs</a>