1

I have a confusion about UML in my class diagram. A person has 2 address, the first is obligatory, the second is not. How do I represent that?

Ghassen
  • 1,039
  • 12
  • 27

2 Answers2

3

You would usually show that with attributes and role names with multiplicity on associations:

enter image description here

See comments regarding the use of attributes/roles on associations.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Notice that according to UML 2.5 you should use ***association end ownership dots***, and not navigability arrows (which are implied by the dots). See http://stackoverflow.com/questions/21321446/is-there-a-free-uml-class-diagram-editor-that-supports-association-end-ownersh – Gerd Wagner Apr 20 '15 at 12:39
  • 1
    Also notice that duplicating properties as an attribute and asssociation end is bad practice and discouraged by the UML spec. – Gerd Wagner Apr 20 '15 at 12:46
  • @gwag that's why i asked this question – Ghassen Apr 20 '15 at 13:09
  • Actually a role on an association and a typed attribute are different renderings of the same thing. I sketched the above with Enterprise Architect which offers role names derived from attributes. From my POV one should use __either__ role or attribute. But since UML does not explicitly forbid the above rendering it's not wrong per se. – qwerty_so Apr 20 '15 at 16:47
  • It's not wrong, but it's considered **bad practice** (because it is redundat). And using navigability arrows for expressing that the association end is owned by the class at the other side (or, in other words, represents a corresponding attribute) is **deprecated** in the UML 2.5 spec (on p.221)..So, do you want to leave your diagram in this form (suggesting a bad practice and using deprecated notation to novices) or improve it? – Gerd Wagner Apr 20 '15 at 18:54
  • @gwag I have removed the direction from the association. – qwerty_so Apr 20 '15 at 20:08
  • 1
    @gwag I just had a look on p. 221 of 2.5. There is no mentioning of deprecation of anything. It's about component diagrams. P.S. Now on p. 209 – qwerty_so May 16 '15 at 12:43
  • @ThomasKilian I don't think you should remove the navigability indicator (arrow). That was not wrong in itself. It seems rather obvious that you should be able to easily navigate to the address if you have access to a person. Whether or not you choose to define ownership is up to you. I think that is often more of a technical nature. So for most business or functional models I would probably not use it. – Geert Bellekens Nov 04 '15 at 19:31
  • @GeertBellekens: I disagree, it's navigability, which is "more of a technical nature" and not suitable for "business or functional models", while association end ownership (despite its dreadful name) simply expresses the equivalence between an associaton end and the corresponding reference property. – Gerd Wagner Nov 04 '15 at 20:10
  • I wonder why nobody argued about the duplication of attribute and role in my example picture. – qwerty_so Nov 04 '15 at 20:44
  • @GeertBellekens Sure, I know. But I know that a lot of people are quite picky here at SO. So just some astonishment ;-) – qwerty_so Nov 05 '15 at 08:43
3

An address should be better not modeled as a class that represents an object type, but as a complex datatype, which is represented as a class rectangle stereotyped with <<dataType>>. For modeling attributes having such a datatype, you wouldn't use any association in the class diagram, but simply use the name of your complex datatype Address as the range/datatype of your attributes primaryAddress and secondaryAddress.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • i think the best way is to use datatype in this case – Ghassen Apr 21 '15 at 08:47
  • Yes, that would be the best approach. In the class model/diagram, you would have two classes: a class `Address` stereo-typed with <>, and a normal `Person` class with two attributes like 'primaryAddress` and ' secondaryAddress', both having `Address` as their type. – Gerd Wagner Apr 21 '15 at 10:52
  • @gwag To be 100%correct the Datatype is not a stereotype on a Class, it is a proper UML metatype. The notation is the same as that of a Class with the addition of the **keyword** `«dataType»` – Geert Bellekens Nov 04 '15 at 19:33
  • @GeertBellekens: Thanks for this clarification of the involved UML semantcs. I guess, however, that practically, using common UML tools, there is no choice of such a metatype, and you have to model it as a class. – Gerd Wagner Nov 04 '15 at 20:04
  • I'm not sure about other tools, but in Enterprise Architect it exists as a separate metatype. – Geert Bellekens Nov 04 '15 at 21:28