Search code examples
classmultidimensional-arrayumlcompositionclass-diagram

How to model 2D array board in UML: associations vs attributes


I have to model a board of elements in UML, something similar to a Chess game, but here the dimension of the board is variable. My problem is when modelling the board. I am modelling it as a two dimensional array of pieces (there is a piece that represents the empty cell).

Here is an example image of what I have:

enter image description here

  • Is this correct?

It seems to me that expressing the relation between Board and Piece both with the piece[][] attribute and with the association is redundant, but otherwise I can't figure out how to represent a two dimensional array just with associations.

  • Is there a way to express a 2D array of elements just with association and multiplicity?

EDIT:

  • The board size depends of the values in the "rows" and "columns" attributes. Is it Ok to express it as a derived attribute?

Solution

  • Is there a way to express a 2D array of elements just with association and multiplicity?

    no, the multiplicity only indicates the number of elements, not how the collection is structured (a matrix/2D-array in your case).

    note it is exactly the same using the notation with an attribute in its compartment in a class, so [8][8]as you probably wanted to do (you also missing the multiplicity inside the []) is not part of the standard.

    In formal/2017-12-05 §9.5.4 page 113 : <property> ::= [<visibility>] [‘/’] <name> [‘:’ <prop-type>] [‘[‘ <multiplicity-range> ‘]’] [‘=’ <default>] [‘{‘ <prop-modifier > [‘,’ <prop-modifier >]* ’}’]<property> ::= [<visibility>] [‘/’] <name> [‘:’ <prop-type>] [‘[‘ <multiplicity-range> ‘]’] [‘=’ <default>] [‘{‘ <prop-modifier > [‘,’ <prop-modifier >]* ’}’]

    So if you want to follow the standard you have to use [64] whatever you use an association or an attribute.

    It seems to me that expressing the relation between Board and Piece both with the piece[][] attribute and with the association is redundant

    In your diagram, forgetting the problem concerning the multiplicity and the fact there is no multiplicity for the association meaning 1, it is not possible to know if the association corresponds to your attribute because the name of the property/memberEnd is missing. Anyway to show both the attribute and the association corresponding to the same property is legal ... even that does not solves your problem.


    After you edit

    If the number of row and columns is not statically known the number of pieces is also variable => the multiplicity to use is [0..*] (or the shortcut [*]) or [1..*] depending if you can have or not no square.

    For me to use a derived attribute is at minimum useless, but worst hide the fact you really have a collection of piece.


    How to say a matrix is used

    The simplest way to indicate you have a matrix of row x column in your diagram is to add a note, of course that way is just informational and cannot be enough in case you also want to generate code but that also depends on the tool you use. So for instance :

    enter image description here

    An other way can be to define a template class for your array (for the notation of the binding see page 26 of formal/2017-12-05) :

    enter image description here