The Composite pattern lets you build tree structures where clients do not need to distinguish between leaves and branches. Every node in the tree responds to the same interface, so operations naturally recurse.
Trade-offs:
The uniform interface may expose methods on leaves that do not make sense (e.g., add() on a File)
Deep trees can cause stack overflows with recursive operations — consider iterative traversal for very large trees
Type checking is harder — all nodes look the same at the interface level
When NOT to use:
When the structure is flat (no nesting) — just use an array
When leaves and composites have very different behaviors — forcing a common interface is awkward
When the hierarchy is dynamic and deeply nested — consider a database or graph instead
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-composite-pattern3description: JS Composite Pattern4---5# JS Composite Pattern67> Compose objects into tree structures and treat individual objects and composites uniformly89## When to Use1011- You have a tree/hierarchy structure (file system, org chart, UI component tree, menu system)12- Clients should treat individual objects and groups of objects the same way13- Operations need to recurse naturally over the tree1415## Instructions16171. Define a component interface with the operation that both leaves and composites share.182. Leaf classes implement the operation directly.193. Composite classes hold a collection of children and delegate the operation to each child.204. Clients call the operation on any component without checking whether it is a leaf or composite.2122```javascript23class File {24 constructor(name, size) {25 this.name = name;26 this.size = size;27 }28 getSize() {29 return this.size;30 }31}3233class Folder {34 constructor(name) {35 this.name = name;36 this.children = [];37 }38 add(child) {39 this.children.push(child);40 return this;41 }42 getSize() {43 return this.children.reduce((sum, child) => sum + child.getSize(), 0);44 }45}4647const root = new Folder('src')48 .add(new File('index.js', 120))49 .add(new Folder('utils').add(new File('helpers.js', 80)));50root.getSize(); // 20051```5253## Details5455The Composite pattern lets you build tree structures where clients do not need to distinguish between leaves and branches. Every node in the tree responds to the same interface, so operations naturally recurse.5657**Trade-offs:**5859- The uniform interface may expose methods on leaves that do not make sense (e.g., `add()` on a File)60- Deep trees can cause stack overflows with recursive operations — consider iterative traversal for very large trees61- Type checking is harder — all nodes look the same at the interface level6263**When NOT to use:**6465- When the structure is flat (no nesting) — just use an array66- When leaves and composites have very different behaviors — forcing a common interface is awkward67- When the hierarchy is dynamic and deeply nested — consider a database or graph instead6869## Source7071https://patterns.dev/javascript/composite-pattern7273## Process74751. Read the instructions and examples in this document.762. Apply the patterns to your implementation, adapting to your specific context.773. Verify your implementation against the details and edge cases listed above.7879## Harness Integration8081- **Type:** knowledge — this skill is a reference document, not a procedural workflow.82- **No tools or state** — consumed as context by other skills and agents.8384## Success Criteria8586- The patterns described in this document are applied correctly in the implementation.87- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-composite-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
JS Composite Pattern It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.