-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Association with multiplicity vs composition #214
Comments
L6 says that there is no definite way to identify composition, and if in doubt leave it out. |
Composition is a whole-part relationship - identified based on your understanding of how two classes are related, not merely based on how the relationship is implemented in code. Just because A has a variable of type B doesn't mean it is composition. The question you should ask is, is there a whole-part relationship between the classes A and B, based on what concepts A and B represent in the application domain. For example, in some applications Car-Engine can be a whole-part relationship while in other applications it may not be (in a car dealership app vs spare parts sales app) |
@pengnam From my understanding a good question to ask whether a relationship should be represented as composition is to ask: will class B objects be deleted if class A is deleted? Your question made me think of an analogy that might help regarding class A having a list of class B objects as an attribute. Scenario 1: modelling a simple book In this scenario, Book objects have a list of Page objects. Logically, if the book is deleted, all the pages are deleted. This is a composition relationship with a multiplicity of *. Scenario 2: modelling a class-student relationship in the academic setting. In this scenario, a Class objects has a list of Student objects. In most contexts, if the Class comes to a close, expires, or closes, the students still exist. This would be modelled as a simple association with multiplicity of *, or aggregation with multiplicity of *. Basically, relationships where B cannot exist independently without A are modelled with composition. Multiplicity provides another dimension of information to the above. |
✔️ However, B cannot exist independently without A should be interpreted based on what each class represents, not how they are implemented. There can be situations where deleting A results in B getting deleted but that is not necessarily a composition.
|
What happens if class A has a list of class B objects as an attribute?
Should we represent it as a composition? Is there any clear way to decide or should we use our intuition?
The text was updated successfully, but these errors were encountered: