microwind/ai-skills/composition-over-inheritance
Claude Code and agent skill by microwind/ai-skills
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
优先使用对象组合而非类继承来实现代码复用,获得更灵活的设计。
Install
npx vskill@latest install microwind/ai-skills/composition-over-inheritanceSKILL.md
组合优于继承 (Composition over Inheritance)
概述
Gang of Four 在《设计模式》中提出:优先使用对象组合而非类继承。继承是"is-a"关系,组合是"has-a"关系。
核心思想:
- 继承创建紧耦合,组合创建松耦合
- 继承在编译时确定,组合在运行时可变
- 继承暴露父类实现细节,组合隐藏内部实现
继承的问题
组合实现多态
TypeScript 中的组合
何时使用继承
最佳实践
| 场景 | 继承 | 组合 | |
|---|---|---|---|
| 代码复用 | ❌ | ✅ | |
| 多态 | ✅ 接口继承 | ✅ 策略模式 | |
| 行为扩展 | ❌ 脆弱基类 | ✅ 装饰器模式 | |
| 运行时变化 | ❌ 编译时固定 | ✅ 可插拔 |
与其他原则的关系
总结
核心原则:优先使用组合,仅在真正的 "is-a" 关系时使用继承。
实践要点:
- 继承用于"是什么",组合用于"有什么"和"能做什么"
- 继承层次不超过 2 层
- 用接口定义行为,用组合注入实现
- 当你犹豫时,选组合
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingdesign-principles/composition-over-inheritance/SKILL.md
Popularity
- Repository stars
- 51
- Repository forks
- 13
- 7-day trend score
- 20
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/microwind/ai-skills/composition-over-inheritance)