Many-to-many relationship in oop

后端 未结 7 763
北恋
北恋 2021-02-04 15:53

what is best way to model many-to-many relationship?

lets say we have a two classes , Team and Player

  • any given P
7条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 16:16

    It's worth to distinguish the API feel from actual implementation.

    While it makes sense for both classes to expose such a collection (e.g. get*List()), they don't neccessarily have to hold the instance of the collection.

    I suggest you create a League class or something alike, that holds some sort of a private player-team mappings dictionary. Additions to those 'collections' thorough the Team/Player instance, should call internal methods on the League instance to update the mappings. This way, you keep updates atomic (as Andrey suggested) and error free.

提交回复
热议问题