dinsdag 22 februari 2011

Can you express order in UML?

Today a collegue asked me if he could express order in a class diagram. Well, you can't.

Situation: class QuestionList, composition of multiple Question items. Actually, each question in the QuestionList is of a specific subtype of Question. How to express that the Question items should be in a specific order?

The Association in the class diagram offers the attribute 'ordered' in the source or target role. This leads to the first set of classes in the diagram below. However, you cannot infer from it that in the QuestionList, an Object of type FirstQuestion should be first, and an object of type SecondQuestion second. Or even that you should have one of each.

Next option is to use Objects instead of Classes. As in the second diagram. It shows that MyQuestionList consists of one object of type FirstQuestion and one of type SecondQuestion, but still no order.

Classes and Objects

The problem is that the order in which questions are to be asked is a dynamic feature, not a static one. And since a Class Diagram is for static constructions, you cannot express order very well. So we go on to a more dynamic diagram: Collaboration. In this diagram you could use the role binding to state which type of question fulfills which role. And then read 'role' as 'being the first question' and so on. See the diagram below. It still isn't machine processable, but at least you have modelled it.

I'm open to better solutions.

Collaboration

Geen opmerkingen: