组合优于继承 logo

组合优于继承

Organization
microwind
组合优于继承

优先使用对象组合而非类继承来实现代码复用,获得更灵活的设计。

Overview

Publishermicrowind
Repositoryai-skills
Skill name组合优于继承
Stars
93
Forks
25
Bundled files
2
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 组合优于继承 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/design-principles/composition-over-inheritance
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/design-principles/composition-over-inheritance .claude/skills/microwind-37
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 组合优于继承 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 组合优于继承 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 组合优于继承 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

组合优于继承 (Composition over Inheritance)

概述

Gang of Four 在《设计模式》中提出:优先使用对象组合而非类继承。继承是"is-a"关系,组合是"has-a"关系。

核心思想:

  • 继承创建紧耦合,组合创建松耦合
  • 继承在编译时确定,组合在运行时可变
  • 继承暴露父类实现细节,组合隐藏内部实现

继承的问题

java
// ❌ 继承导致的问题:脆弱基类
public class ArrayList<E> {
    public void add(E element) { /* ... */ }
    public void addAll(Collection<E> c) {
        for (E e : c) add(e);  // 内部调用 add
    }
}

public class CountingList<E> extends ArrayList<E> {
    private int addCount = 0;

    @Override
    public void add(E element) {
        addCount++;
        super.add(element);
    }

    @Override
    public void addAll(Collection<E> c) {
        addCount += c.size();
        super.addAll(c);  // 父类 addAll 又调用了 add,计数翻倍!
    }
}
java
// ✅ 组合:不依赖父类实现细节
public class CountingList<E> {
    private final List<E> list = new ArrayList<>();
    private int addCount = 0;

    public void add(E element) {
        addCount++;
        list.add(element);
    }

    public void addAll(Collection<E> c) {
        addCount += c.size();
        list.addAll(c);  // 不关心 ArrayList 内部如何实现
    }

    public int getAddCount() { return addCount; }
}

组合实现多态

python
# ❌ 多重继承导致菱形问题
class Flyer:
    def move(self):
        return "飞行"

class Swimmer:
    def move(self):
        return "游泳"

class Duck(Flyer, Swimmer):  # 菱形继承,move() 是哪个?
    pass

# ✅ 组合:行为可插拔
class FlyBehavior:
    def move(self) -> str:
        return "飞行"

class SwimBehavior:
    def move(self) -> str:
        return "游泳"

class Duck:
    def __init__(self):
        self.fly_behavior = FlyBehavior()
        self.swim_behavior = SwimBehavior()

    def fly(self) -> str:
        return self.fly_behavior.move()

    def swim(self) -> str:
        return self.swim_behavior.move()

TypeScript 中的组合

typescript
// ❌ 继承层次过深
class Animal { move() {} }
class Bird extends Animal { fly() {} }
class Parrot extends Bird { talk() {} }
class PetParrot extends Parrot { tricks() {} }
// 4 层继承,耦合严重

// ✅ 组合 + 接口
interface Movable { move(): void; }
interface Flyable { fly(): void; }
interface Talkable { talk(): void; }

class Parrot implements Movable, Flyable, Talkable {
    constructor(
        private mover: Movable,
        private flyer: Flyable,
        private talker: Talkable
    ) {}

    move() { this.mover.move(); }
    fly() { this.flyer.fly(); }
    talk() { this.talker.talk(); }
}

何时使用继承

继承仍然适用的场景:
✅ 真正的 "is-a" 关系(Cat is an Animal)
✅ 框架要求(Android Activity、JUnit TestCase)
✅ 模板方法模式(固定算法骨架,子类实现步骤)
✅ 类层次稳定且不深(≤ 2 层)

应该用组合的场景:
✅ "has-a" 或 "uses-a" 关系
✅ 需要运行时切换行为
✅ 需要复用多个不相关类的功能
✅ 继承层次超过 2 层

最佳实践

场景继承组合
代码复用❌✅
多态✅ 接口继承✅ 策略模式
行为扩展❌ 脆弱基类✅ 装饰器模式
运行时变化❌ 编译时固定✅ 可插拔

与其他原则的关系

原则关系
LSP违反 LSP 时通常应改用组合
OCP组合通过注入新行为实现 OCP
DIP组合 + 接口 = DIP 的典型实现
ISP小接口 + 组合 = 灵活的设计

总结

核心原则:优先使用组合,仅在真正的 "is-a" 关系时使用继承。

实践要点:

  • 继承用于"是什么",组合用于"有什么"和"能做什么"
  • 继承层次不超过 2 层
  • 用接口定义行为,用组合注入实现
  • 当你犹豫时,选组合

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 组合优于继承 AI skill do?

优先使用对象组合而非类继承来实现代码复用,获得更灵活的设计。

Why use 组合优于继承 on TypingMind?

Because you install it once and use it with any model. 组合优于继承 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 组合优于继承 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/design-principles/composition-over-inheritance. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 组合优于继承?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 组合优于继承?

As many as you like. As long as a model supports skills, you can use 组合优于继承 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 组合优于继承 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇