1

I have a simple concept like in the image below. Basically there is an Item which can be 1 of 3 types and a character that can be 1 out of 2 types. All generalizations are disjointed.

enter image description here

What I'm trying to do is to somehow depict with UML notation that a Character can at any given time carry only one item but Monk can carry only a book or a staff, while Paladin can carry any kind of item (Book, Sword or a Staff).

I can't figure out how to associate these classes in this way to preserve the aforementioned constrains. Is it even possible with this UML composition?

2

First correction (recommended):

First, UML is not Java. The relation between Book, Sword or Stuff classes on one side and Item class on the other is shown with a generalization, and there's no need to label the arrow with Extends. There's no confusion possible in this UML diagram with a realization relation (i.e. "implements"), since the latter would be shown with a dotted line.

Second correction (required):

Your narrative is inconsistend with your diagram. You mention in your question a Character that can be a Monk or a Palladin, but your diagram suggest that a Palladin in a kind of Monk (According to the generalization relation that you display, Monk would be the more general concept, and Palladin would be a more specialized kind of Monk). This is confusing.

Starting point

So your starting diagram could look like:
enter image description here

Relationship you want to show:

You want to show that a Character can have at any time only one Item. So in practice, this means that you have a relation between a Character and zero or one (0..1) Items that can be read as "owns" (the character owns an item). Conversely, an Item can be owned by zero or one Character.

This would be shown as follows:
enter image description here

Unfortunately, this would let any character own any kind of items. If you want to restrict this, you need to use an UML constraint. This is done by using a comment box and put a constraint between curly brackets {...}. The formal way to do would be to use OCL language. But a more practical approach is to document the contraint in plain letters:

enter image description here

Here, the contraint is shown on the association. It's practical, since you could add other constraints for other kind of characters. However, you could also opt to show the constraint for the Monk class instead (and other classes that have their associations constrained).

  • Thank you for your reply, it was very helpful. But I was wondering if it's possible to achieve the same result without OCL. For example, would this class diagram work: i.imgur.com/chqo5Aw.png . I'm creating 2 abstract classes for different item types and override the property on Monk that that he could only carry items of a certain type. – Regs Nov 4 '18 at 18:55
  • 1
    @Regs yes, you can express it this way. However this is a different semantic: Monk can have some items, palladin can have an item of a more general kind. But character doesn't have anything. This means that the code of the general Character can't know about/deal with owned items. – Christophe Nov 4 '18 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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