Static List of all Objects of Class

2019-08-20 12:39发布

问题:

I wanted to implement a static List for my Class which contains all Objects of the Class like in the following UML approach.

Is this the right way to write the UML?

There is also a question which uses this UML if you know Kotlin i would be happy if you can help me there too ;) Static List of Objects out Class in Kotlin

回答1:

This is almost correct. Instead of List<IMyClass> the regular UML notation is IMyClass[*].

The UML 2.5.1 specification, section 9.5.4 gives the following notation syntax for a property:

<property> ::= [<visibility>] [‘/’] <name> [‘:’ <prop-type>] [‘[‘ <multiplicity-range> ‘]’] [‘=’ <default>] [‘{‘<prop-modifier > [‘,’ <prop-modifier >]* ’}’]

In your case, the prop-type is IMyClass and the multiplicity-range is *.



回答2:

Use the stereotype to specify you use a list, for instance :

or if you prefer :

Note : are you sure you want it public ? this is dangerous


Edit for the remark of @qwerty_so: it does not make sense to stereotype an association with <<list>> where you place a multiplicity which makes it a "list" already. Looks like an over-definition too me

The multiplicity indicates a collection, and there are four types of collection depending on isOrdered and isUnique properties (c.f. formal-17-12-05 §7.5.3.2 Multiplicities, Table 7.1 Collection types for MultiplicityElements, page 34) :

isOrdered  | isUnique | Collection Type
-----------+----------+-----------------
   false   |   true   |      Set
    true   |   true   |   OrderedSet
   false   |  false   |      Bag
    true   |  false   |    Sequence

Furthermore a list is one of the subtypes of Sequence, so the multiplicity "*" is far from indicating a "list" already.



标签: uml