how do I add a final variable to class diagram

前端 未结 3 1770
独厮守ぢ
独厮守ぢ 2021-02-13 17:43

I am designing a class diagram for scrabble game. In one of the classes, I have final variable declared. Can anybody tell me, how can I indicate a variable as final in the UML c

相关标签:
3条回答
  • 2021-02-13 18:19

    Constant (i.e. final) fields are indicated via naming convention: constants should be in ALL_CAPS

    Source

    0 讨论(0)
  • 2021-02-13 18:20

    Declaring a variable/attribute final is implementation detail. So you don't need to specify it in your CLASS Diagram but you can follow the convention as suggested by eboix.

    UML specification doesn't say anything specifically about it; so you can follow convention of showing it in ALL CAPS.

    0 讨论(0)
  • 2021-02-13 18:35

    There are different notions of final that are all represented in different ways:

    final definition, i.e. it cannot be overridden in sub-classes - this corresponds to the isLeaf property of the attribute:

    isLeaf: Boolean - Indicates whether it is possible to further redefine a RedefinableElement. If the value is true, then it is not possible to further redefine the RedefinableElement. (UML Superstructure, p. 132)

    There is no official notation for attributes with isLeaf=true, but adding "{leaf}" is common.

    final value, i.e. its value cannot be changed - this corresponds to the isReadOnly property of the attribute:

    isReadOnly : Boolean - If true, the attribute may only be read, and not written. The default value is false. (UML Superstructure, p. 125)

    Notation for read-only attributes consists of appending "{readOnly}" to the attribute string.

    constant usually refers to a non-changeable attribute of the class itself instead of an instance (static final attribute). In UML it would have both properties mentioned above and additionally be static, which corresponds to the isStatic property:

    isStatic: Boolean - Specifies whether this feature characterizes individual instances classified by the classifier (false) or the classifier itself (true). Default value is false. (UML Superstructure, p. 69)

    Static attributes are indicated by underlining the attribute definition. Constants, as already mentioned are usually UPPERCASE, but that's just a convention.

    So, to sum it up, a constant attribute FOO of type String with value "x" would look like this and be underlined in addition (which isn't supported here):

    + FOO : String = "x" {readOnly,leaf}

    0 讨论(0)
提交回复
热议问题