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?
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.
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).Sender
and a Receiver
class, with Email
being an association classSender
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:
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.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.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 ?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.