Composite
Also known as: Object Tree
Purpose
Help the user decide whether Composite is appropriate, then design and implement tree-shaped object models that let clients treat simple and complex nodes uniformly.
When To Use
- User asks to explain Composite or Object Tree pattern.
- Domain can be modeled naturally as a recursive tree (nodes with child nodes).
- Client should invoke the same operations on both leaves and containers.
- Logic requires recursive aggregation (price totals, rendering, permissions, metrics, traversal).
- Feature growth needs extensible node types without rewriting client code.
Inputs
- Core domain entities and parent/child relationships.
- Candidate component operations shared by leaves and containers.
- Aggregation semantics (sum, merge, short-circuit, fold).
- Tree constraints (acyclic, max depth, ordering, identity, mutability rules).
- Runtime concerns (performance, traversal strategy, concurrency, serialization).
Workflow
- Clarify the goal.
Confirm whether the user needs conceptual guidance, architecture review, or implementation/refactor.
- Check applicability.
Use Composite when the model is genuinely hierarchical and clients need uniform node handling.
- Define component contract.
Extract operations meaningful for both leaves and containers.
- Model leaves.
Implement concrete leaf classes that perform actual domain work directly.
- Model containers.
Implement composite/container classes that store child components and delegate recursively.
- Add child management boundaries.
Define add/remove/list behavior in container (and optionally on component with no-op/guard in leaves).
- Refactor client usage.
Replace type checks and special-case branching with component-polymorphic calls.
- Protect tree integrity.
Enforce no cycles, valid parent links, and domain invariants.
- Validate recursive behavior.
Verify correctness for deep nesting, empty containers, and mixed node types.
Decision Branches
- If structure is not tree-like or requires graph semantics with shared parents:
Prefer explicit graph model over Composite.
- If only one wrapped child is needed to extend behavior:
Prefer Decorator.
- If operations frequently change but node structure is stable:
Consider Visitor for operation extension.
- If traversal behavior needs abstraction from structure:
Consider Iterator with or without Composite.
- If child-management methods on leaf violate interface clarity for your domain:
Keep child operations only on container and accept non-uniform construction API.
Output Contract
When responding, provide:
- A suitability verdict (Composite or better alternative).
- Tree model summary (component, leaf types, container types, child rules).
- Role mapping (Client, Component, Leaf, Container/Composite).
- Incremental migration plan from branching/type-check code to recursive polymorphism.
- Minimal code sketch in the user's language showing one leaf and one container operation.
- Validation checklist proving recursive correctness and tree integrity.
Quality Checks
- Component interface includes only truly shared operations.
- Containers delegate to children and aggregate deterministically.
- Leaves do not depend on container internals.
- Tree invariants prevent cycles and invalid parent-child references.
- Client code works through component abstraction, not concrete type checks.
- Deeply nested and empty-tree edge cases are covered by tests.
Common Mistakes To Prevent
- Forcing unrelated operations into component interface.
- Mixing business aggregation logic into clients instead of container recursion.
- Allowing cyclic references that cause infinite recursion.
- Overusing Composite for flat data where simple collections are enough.
- Coupling container behavior to specific leaf subclasses.
References
1---2name: composite3description: Teach and apply the Composite pattern to model tree structures where leaves and containers share a common interface and can be handled uniformly. Use when users ask about Composite or Object Tree, recursive aggregation, uniform leaf/container operations, or comparing Composite with Decorator, Visitor, or Iterator.4---56# Composite78Also known as: Object Tree910## Purpose11Help the user decide whether Composite is appropriate, then design and implement tree-shaped object models that let clients treat simple and complex nodes uniformly.1213## When To Use14- User asks to explain Composite or Object Tree pattern.15- Domain can be modeled naturally as a recursive tree (nodes with child nodes).16- Client should invoke the same operations on both leaves and containers.17- Logic requires recursive aggregation (price totals, rendering, permissions, metrics, traversal).18- Feature growth needs extensible node types without rewriting client code.1920## Inputs21- Core domain entities and parent/child relationships.22- Candidate component operations shared by leaves and containers.23- Aggregation semantics (sum, merge, short-circuit, fold).24- Tree constraints (acyclic, max depth, ordering, identity, mutability rules).25- Runtime concerns (performance, traversal strategy, concurrency, serialization).2627## Workflow281. Clarify the goal.29 Confirm whether the user needs conceptual guidance, architecture review, or implementation/refactor.302. Check applicability.31 Use Composite when the model is genuinely hierarchical and clients need uniform node handling.323. Define component contract.33 Extract operations meaningful for both leaves and containers.344. Model leaves.35 Implement concrete leaf classes that perform actual domain work directly.365. Model containers.37 Implement composite/container classes that store child components and delegate recursively.386. Add child management boundaries.39 Define add/remove/list behavior in container (and optionally on component with no-op/guard in leaves).407. Refactor client usage.41 Replace type checks and special-case branching with component-polymorphic calls.428. Protect tree integrity.43 Enforce no cycles, valid parent links, and domain invariants.449. Validate recursive behavior.45 Verify correctness for deep nesting, empty containers, and mixed node types.4647## Decision Branches48- If structure is not tree-like or requires graph semantics with shared parents:49 Prefer explicit graph model over Composite.50- If only one wrapped child is needed to extend behavior:51 Prefer Decorator.52- If operations frequently change but node structure is stable:53 Consider Visitor for operation extension.54- If traversal behavior needs abstraction from structure:55 Consider Iterator with or without Composite.56- If child-management methods on leaf violate interface clarity for your domain:57 Keep child operations only on container and accept non-uniform construction API.5859## Output Contract60When responding, provide:611. A suitability verdict (Composite or better alternative).622. Tree model summary (component, leaf types, container types, child rules).633. Role mapping (Client, Component, Leaf, Container/Composite).644. Incremental migration plan from branching/type-check code to recursive polymorphism.655. Minimal code sketch in the user's language showing one leaf and one container operation.666. Validation checklist proving recursive correctness and tree integrity.6768## Quality Checks69- Component interface includes only truly shared operations.70- Containers delegate to children and aggregate deterministically.71- Leaves do not depend on container internals.72- Tree invariants prevent cycles and invalid parent-child references.73- Client code works through component abstraction, not concrete type checks.74- Deeply nested and empty-tree edge cases are covered by tests.7576## Common Mistakes To Prevent77- Forcing unrelated operations into component interface.78- Mixing business aggregation logic into clients instead of container recursion.79- Allowing cyclic references that cause infinite recursion.80- Overusing Composite for flat data where simple collections are enough.81- Coupling container behavior to specific leaf subclasses.8283## References84- Detailed guide: [REFERENCE.md](./REFERENCE.md)85- Worked examples: [EXAMPLES.md](./EXAMPLES.md)