Implementing Connections within Aggregation classes

陌路散爱 提交于 2019-12-24 02:14:12

问题


In the below diagram, the class "Copy" and class "Book" are showing an aggregation (Book has a Copy). All other classes are sub classes of class "Copy". Now, imagine there is a Member class, which has to access the sub classes "Lending" and "Borrow" of class "Copy". The only thing that 'Member' class should know is "accessionNumber".

If we think of this in terms of code, my "Book" class contains an array of "Copy".

How can I know the accessionNumber of a particular book?

Lets say I need the "accessionNumber" of the 3rd book in my copies array. But the array is in the Book class, and there is no connection to the Book class from the Copy class.

If we create a connection back, would that be like "Copy has a Book", or more meaningfully, something like "Subject has Student" (taking the famous aggregation example "Student has a Subject)?


回答1:


If we create a connection back, would that be like "Copy has a Book"

No, it wouldn't. A simple dependency relationship (e.g. a reference or non-owning pointer in C++) is not the "has a" or "owns a" relationship you'd get from the stronger associations of aggregation or composition. That sounds like what you want here.



来源:https://stackoverflow.com/questions/14505463/implementing-connections-within-aggregation-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!