问题
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