How to set sequence relationship between classes?

2019-07-02 22:57发布

问题:

I am newbie in UML modeling, and this might seem to be a silly question to some but I will give a try.

Please consider the following picture:

How to specify the order in which they will appear into the calendar. The fact that Monday is after Sunday and before Tuesday, matters in this model. But how to transfer that information to the reader of the model? Thank you.

回答1:

Perhaps Monday is one of seven instances of a day of the week, not a kind of day of the week. Think of the class Day Of Week as a set. What are the members of the set? There are seven: {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}. Now think of the class Monday. Do you want members {this-past-monday, next-monday, ...}? Probably not.

You could go with seven instances of the class Day of Week, or you could use an enumeration with seven enumeration literals. In either case, you still have the problem of ordering the instances, but at least with instances or enumeration literals, the two meanings of is a aren't mixed up. (As in, is an instance of vs. is a kind of.)

In UML you can have a property that has {ordered} values of type Day Of Week. You don't get to say how they're ordered, though.

Below I've worked two examples. One is using a class, which you would instantiate with seven instances, each having a unique value for the sequenceNumber attribute, which you can use to order the instances. The other is using an enumeration, which has seven enumeration literals. Each enumeration literal has an attribute called sequenceNumber, which you can use to order the enumeration literals.

Each enumeration literal is an instance of the enumeration and has a slot containing a unique value. These slots cannot be shown on a diagram, but they are in the model. Here is a screenshot showing the slot for each enumeration literal in the containment tree for the model:



回答2:

There is no order. If you want to create an enumeration then you have to create a <<enumeration>> stereotyped class Day Of Week and put the days in as single attributes.

Your model just defines that Monday etc. are weekdays.

Note: Taking into account JimL.'s comment, this is neither a stereotyped class, nor does it contain attributes. It's a datatype with literals. Thanks OMG you made it look just the same ;-)



标签: uml