堆栈和子类之间正确的 UML 关系是什么?

What is the correct UML relationship between a Stack and SubClasses?

我环顾四周试图弄清楚 Stack 和 SubClasses 之间的正确关系对于这种实现是什么:

struct Animal {
    virtual void feed() = 0;
};

struct Dog : Animal {
    void feed() {
        cout << "bark";
    }
};

struct Cat : Animal {
    void feed() {
        cout << "meow";
    }
};

struct AnimalStack {
    Animal *array[10];

    //rest of implementation for the stack
};

int main() {
    //create Stack
    //create new Animal objects and push them to the AnimalStack
}

我认为 Stack 和 Animal 之间的关系是关联关系,但我也看到有人为类似的问题争论依赖关系。

这将是 聚合 的示例。 https://en.wikipedia.org/wiki/Object_composition#Aggregation

  • 堆栈中有动物
  • 没有明确的所有权

依赖关系的论点是一个弱论点,并没有暗示 Stack 存储(指向)Animals。它没有以有意义的方式描述代码。

Animal 有子类的事实是模型的一个独立部分,不会改变 Stack -> Animal 关系。