Search code examples
oopumlcomputer-sciencesoftware-design

Relationship among UML classes


I have to make a Class Diagram of an e-mail. After the abstraction, I was able to create 2 classes: Sender and Recipient. At first I thought the relation between them would be an aggregation by composition, since there's not an e-mail without one of those 2 classes. At the same time, when these 2 classes communicate with each other, another one comes up: E-mail. This class is the link/connection of those 2 first ones and, by realizing that, the association is in fact a ternary association. Am I right or I'm missing something? Class Diagram of an e-mail


Solution

  • In short

    Yes, a ternary association is a possibility. But think of it twice, because many people have difficulties reading ternary associations with hight multiplicity on one branch only.

    More details

    There are plenty of ways to model the relationship between an email, a sender, and multiple receivers. There is no single good answer. For example:

    • Email class with associations with Receiver and Sender
    • Email class with an aggregation with Reveiver and Sender if you see those as a part of an Email which can be shared (between several emails referring to the same senders or receivers).
    • Email class with a composition with Reveiver and Sender if you'd see each sender or receiver as an embedded value object within the emails (so two emails with the same sender address would each have its own copy of the object).
    • A Sender and a Receiver class, with Email being an association class
    • A ternary association as you have modelled it,
    • Not to speak of the case were you would have Sender and Receivers beign composed with or inheriting from a common denominator EmailAccount.

    The question is how you want to see all these elements, i.e. your understanding of the world. For example:

    • who send() the Email: is it the Sender or is it the Email or is it the mailbox? It's not clear in your model where this responsibility lies.
    • can an Email exist without a Sender or a Receiver? I often start to write an email and add the receivers later on (to avoid sending it by accident). If the answer is yes, you elminiate the association class.
    • is Receiver really part of your system? If no, why is there a read() opeartion? If yes, what's the benefit of having a single system email system ?
    • how easy do you think the concept of ternary association is to grasp for your readers, whent tehre is a higher multiplicity on one end?

    And this leads to the question of what your system really do with emails. It would be a pitty to model complex relationships with Senders and Receivers if in the end those are not needed, because you have just to develop an MTA that needs to get the Receivers from an email and make some queries to forward the email to the correct IP addresses.

    Maybe a good start for your modeling could therefore be RFC 5322 which explains that senders and receivers are in fact only email addresses, which would lead to a simplified model with an Email class, and Address class, and two associations (one with end to and the other with end from) between the same classes.