3

Note: this question is different from Can a UML class diagram show two different relationships between two classes? which is about two relations of different type.

I have a Menu class which contains:

  • A bunch of children, which are instances of MenuItem class.

  • An instance of a MenuItem class which corresponds to a very specific menu item (and is not necessarily set for every instance of Menu class.)

How am I expected to represent such relation? In other words, is this representation correct:

enter image description here

or should I remove the second aggregation as being redundant?

3
  • I personally would say "why not?" but my expertise level doesn't make me feel very confident. By the way, I think that MenuItem's lifecycle is tightly bound to Menu's. Why not use compositions instead of "simple" aggregations Commented Sep 11, 2015 at 10:11
  • @C.Champagne: in the context of the project, menu items are initialized before the menu, thus the usage of aggregations. This being said, in the context-less example from my question, compositions make indeed much more sense. Commented Sep 11, 2015 at 11:46
  • @MainMa, you may use a composition even if the MenuItem exists earlier than the Menu. Commented Jan 27, 2016 at 21:00

1 Answer 1

3

In your description, you are talking about two ways in which a Menu instance contains MenuItem instances: A set of children and a separate customization item.
This implies that there are two separate, independent, relations between Menu and MenuItem and that each of them plays an important role in the design.

Your UML diagram should contain all the relations that are needed to convey the ideas of your design. If you omit a relation here, then you are removing important information from your diagram and you will likely end up with software that does not match what you intended to build.

The main difference here with the question you linked to (Can a UML class diagram show two different relationships between two classes?) is that there one of the relations (the nesting of the classes) can be regarded as incidental and not representing a core design concept. Such incidental relations can be omitted from a diagram if they would mostly create clutter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.