How to model a Many to many-relationship in code?

前端 未结 12 1970
星月不相逢
星月不相逢 2020-12-05 07:12

Suppose I have 2 tables in a database. eg: Dog & Boss This is a many to many relationship, cause a boss can have more than 1 dog, and a dog can have more than 1 owner. I

12条回答
  •  离开以前
    2020-12-05 07:21

    Why are you talking about tables? Are you creating an object model or a database model?

    For an object model, there's no reason a Dog can't have a List and an owner have a List. Only if you have attributes on the relationship do you need an intermediate class (what UML calls an Association Class). That's when you'd have a DogOwnership class with extra properties, and each Owner would have a List, and so would each Dog. The DogOwner would have a Dog, an Owner, and the extra properties.

提交回复
热议问题