Skip to content
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

Open
pengnam opened this issue Dec 4, 2018 · 4 comments
Open

Association with multiplicity vs composition #214

pengnam opened this issue Dec 4, 2018 · 4 comments

Comments

@pengnam
Copy link

pengnam commented Dec 4, 2018

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?

@pengnam
Copy link
Author

pengnam commented Dec 4, 2018

L6 says that there is no definite way to identify composition, and if in doubt leave it out.

@damithc
Copy link
Contributor

damithc commented Dec 4, 2018

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)

@aidenywl
Copy link

aidenywl commented Dec 4, 2018

@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
A: Book
B: Page

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.
A: Class
B: Student

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.
Relationships where B can exist independently with A are modelled with association (or aggregation in certain cases).

Multiplicity provides another dimension of information to the above.

@damithc
Copy link
Contributor

damithc commented Dec 4, 2018

Basically, relationships where B cannot exist independently without A are modelled with composition.
Relationships where B can exist independently with A are modelled with association (or aggregation in certain cases).

✔️

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.
In other words

  1. A♦ ----B means B gets deleted when A is deleted.
  2. B gets deleted when A is deleted does not necessarily mean A♦ ----B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants