2

I am making a class diagram. I have a Person class and an Address class. I am thinking there is a 'Has-A' relation between the Person class and Address class (Aggregation):

  1. Am I right in marking the relationship as association?

  2. Does it depend on us how we want the model the relationship? For example, if I have two classes, Book and Library, I could say that Books shall not exist without a Library (composition) or I could say that Books may exist independent of library(Aggregation).

Christophe
  • 68,716
  • 7
  • 72
  • 138
back2back
  • 33
  • 6
  • Does this answer your question? [What is the difference between association, aggregation and composition?](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) – Geert Bellekens Jul 05 '22 at 08:51

2 Answers2

3
  1. There is no one correct answer. There are many valid ways to model a scenario. In this case you could either mark the relationship between Person and Address as an association (more specifically aggregation), or you could mark it as a complex attribute.

  2. Yes, details like that should be discussed with stakeholders / people who understand the domain you are modelling.

Redseb
  • 199
  • 10
  • 1
    True! And it also depends on the purpose of the class diagram. Is it a real world model? Is it a functional application design? Is it a database design? Is it a Java or C# class design? If it is a real world model, then an address shall not be aggregated under person. My address is not a part of me. – www.admiraalit.nl Jul 05 '22 at 14:43
  • @www.admiraalit.nl I’ confused by your last remark. Is there any quote in the UML specs that gives a part-whole semantic to a shared aggregation? – Christophe Jul 05 '22 at 17:33
  • @Christophe: No, but the meaning of the word 'aggregation' in English implies that items are put together to form some kind of group (the aggregate). I don't think that in the real world, you would say that a person is an aggregation of an address and other things and therefore, I think you shall not model address aggregated under person in a real world model. – www.admiraalit.nl Jul 06 '22 at 10:12
  • @www.admiraalit.nl I think we agree. It’s just that I’d even go further and always avoid shared aggregation, since doesn’t add anything useful (https://stackoverflow.com/q/70316119/3723423) – Christophe Jul 06 '22 at 15:57
2

Is the association right?

Yes, you are right: a simple association expresses perfectly that a Person has an Address. Nobody could claim on the base of your narrative that your model would be wrong.

But modeling is a form of communication: You may well chose a different notation to add nuance in what you express, and you may decide for different semantics to tell how you see things in view of your needs.

Does it depend on us? Notation

In our example, you may want to clarify what you mean with the association by giving it a name:
enter image description here

Or you may prefer to clarify the role of the address in the association:
String

Or in complex diagrams you may prefer the shorter but equivalent property notation, nevertheless keeping in mind that "A useful convention for general modeling scenarios is that a Property whose type is a kind of Class is an Association end, while a property whose type is a kind of DataType is not":

enter image description here

Does it depend on us? Semantics

You could go for aggregation, but I'd strongly discourage it since UML does not define any semantic for it. So there's no benefit compared to a simple association.

You could consider composition. It might in general not be the best choice, as addresses exist independently of the persons. But in an application that creates separate Address objects for each Person, it could reveal how you intend to manage addresses.

Or you may want some richer semantics, for example with an association class to tell that people could have plenty of addresses of different kinds:

enter image description here

Christophe
  • 68,716
  • 7
  • 72
  • 138