design-pattern-composite
Use when clients must treat individual objects and nested object groups uniformly through one interface.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Skill: Composite
Intent
Represent part-whole hierarchies as tree structures so clients can operate on individual elements and compositions using the same interface.
Applicability Signals
- Signal 1: Domain naturally forms recursive tree structures (UI widgets, file systems, org charts, expression trees).
- Signal 2: Client logic branches repeatedly between leaf handling and collection handling.
- Signal 3: Operations should apply uniformly at any hierarchy depth.
Contraindications
- Case 1: Structure is flat and unlikely to become hierarchical.
- Case 2: Leaves and containers require radically different APIs with little overlap.
- Case 3: Tree mutability/performance constraints make generic traversal too expensive.
Decision Heuristics
- If recursive part-whole modeling is central and clients need uniform treatment, prefer Composite.
- If you only need traversal behavior over an existing structure, Iterator may be enough.
- Decision anti-bias note: avoid forcing uniform APIs when domain semantics differ sharply.
Implementation Checklist
- Define component interface for shared operations.
- Implement leaf with direct behavior.
- Implement composite holding child components and delegating/aggregating operations.
- Define child-management operations with clear ownership rules.
- Add recursive tests for depth, ordering, and failure handling.
Misuse Checks
- Misuse 1: Leaf receives irrelevant child-management methods → Remediation: keep child management only where needed or use safe no-op policy.
- Misuse 2: Composite violates invariants (cycles, duplicate ownership) → Remediation: enforce parent/child integrity checks.
- Misuse 3: Clients depend on concrete leaf/composite checks frequently → Remediation: move behavior into component interface.
Verification Rubric
- Correctness:
- Leaf operation works standalone.
- Composite operation correctly aggregates/delegates across nested children.
- Design quality:
- Component interface captures meaningful shared behavior.
- Hierarchy manipulation rules are explicit and validated.
- Regression safety:
- Tests cover single node, multi-level tree, and invalid hierarchy edge cases.
Language-Specific Adaptations (Optional)
- TypeScript: model
Componentinterface and recursive child arrays with readonly where possible. - Python: dataclass-based leaves/composites can keep tree structure clear.
- Go: use interface + struct composition; guard against cycles in add-child operations.
Related Patterns (Optional)
- Iterator: traverse composite trees without exposing internal representation.
- Visitor: add operations across composite structures without modifying component classes.
Attribution & Sources
- Source Site: Refactoring.Guru
- Source URLs:
- Derivation Note: Concepts derived from referenced sources; explanatory wording rewritten for this repository.
- Policy Note: This artifact intentionally includes no direct quotes and no Refactoring.Guru images.
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-03-11
- 索引更新时间
- 2026-03-11
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!