6

I'm drawing a class diagram and noticed that sometimes I got a relationship (composition or aggregation) where the item uses the container, and sometimes not.

What is the visual difference in a composition where the item class uses the container class vs where the item class doesn't know about the container class?

Gangnus
  • 2,805
  • 4
  • 21
  • 31
p1100i
  • 1,748
  • 1
  • 10
  • 11
  • Just add an arrow back from the child to the parent when the child does know about the parent? – Marjan Venema Aug 07 '13 at 10:07
  • @MarjanVenema: The parent side is where the diamond is. You can't add an arrowhead there. – Jan Hudec Aug 07 '13 at 10:54
  • @JanHudec: no of course not, but you can add a _new_ arrow to denote the relationship from child to parent. – Marjan Venema Aug 07 '13 at 11:01
  • 1
    @MarjanVenema That would be very non-standard UML. It might make sense to a person reading it if it was drawn that way on a whiteboard or thrown up as a picture somewhere, but wouldn't work for automated generation of code from a model, and it's probably not what the model would look like if it was reversed engineered from an implementation. – Thomas Owens Aug 07 '13 at 11:34
  • @ThomasOwens: Hmm, okay, I guess I would find it more intuitive / explicit than "knowing" that the absense of the arrowhead means the relation is bidrectional. Looking at an uml diagram without this knowledge, it would never occur to me that that is what it signifies (or, reading comments on your answer, rather what it _may_ signify) – Marjan Venema Aug 07 '13 at 12:18
  • @ThomasOwens: Out of curiosity, if absense of the arrow means that the direction of the relation is unspecified, how do these code generation tools deal with it? Do they barf because it is unspecified? Or assume it is bi-directional? If the first, the question remains how to specify bi-directionality in the diagram? – Marjan Venema Aug 07 '13 at 12:28
  • @MarjanVenema It's somewhat explained in the comments on my answer. Formally, a lack of an arrow indicating directionality indicates bidirectionality and you need an arrow to indicate directionality. However, in less than formal UML (such as quick sketches), it may be left off and one needs other "hints" to determine what is precisely meant. – Thomas Owens Aug 07 '13 at 23:10
  • @ThomasOwens: Thanks for the clarification. Learned something and that always makes my day :) – Marjan Venema Aug 08 '13 at 06:41

1 Answers1

7

You can add an open arrow head to the composition or aggregation line, pointing toward the child to indicate directionality in the relationship. If the child knows about the parent, then simply don't include an arrow at all. I believe this works, because composition is a stronger form of an association link. The diamond indicates a composition or aggregation relationship. Without an arrow head, it can be read as either bidirectional or unspecified. Adding an arrow head makes it specific that the relationship is one-way.

Keep in mind that it doesn't make sense for a parent to not know about its child since these relationships indicate a parent maintaining a collection of children, so an arrow head pointing to the parent doesn't make sense. The only two reasonable relationships are bidirectional (parent knows about child, child knows about parent) and a "parent knows about child, but child doesn't know about parent" directional.

Directionality, indicating that the child does not know about the parent:

directionality in association and composition

Bidirectionality, indicating that the parent knows about the child (required for a composition or aggregation relationship) and the child knows about the parent, or indicating an unspecified relationship (I would expect the details about the child's member variables and methods to provide hints):

bidirectionality in association and composition

Thomas Owens
  • 79,623
  • 18
  • 192
  • 283
  • I agree with you that the presence of an arrow head means that there is a one-directional relationship. However, the absence of an arrow head does not always mean that there is a two-directional relationship. Depends on how strict of loose the team follows standards. I prefer to not be too strict. The point of UML is communication, and if the absence of arrowhead makes for an easier to understand diagram, I would omit it. – Pete Aug 07 '13 at 10:18
  • @Pete I explicitly said that the absence of an arrowhead may mean that the relationship is unspecified - it may be bidirectional or one directional. The next step would be to look at the details in the model of the child class to see the member variables or arguments to constructors or methods to see if the parent is either maintained as state or used as an input or not. If it's not modeled at that level of granularity, it's not possible to fully define the relationship based only on the model (or it's up to the implementer of the model to decide how to implement, based on requirements). – Thomas Owens Aug 07 '13 at 10:22
  • 1
    @ThomasOwens so you're saying, that there is no way to explicitly visualize that the child knows about the parent, however if I leave it unspecified it can mean that? – p1100i Aug 07 '13 at 11:53
  • @burninggramma No arrow head would, formally, only indicate that there is a bidirectional relationship. However, UML is often used informally and the arrow head is left off even in a unidirectional relationship. – Thomas Owens Aug 07 '13 at 16:38
  • @ThomasOwens I see, make sense now, ty for answer! – p1100i Aug 07 '13 at 17:38