how do I add a final variable to class diagram

江枫思渺然 提交于 2021-02-06 15:25:21

问题


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 class diagram?


回答1:


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

Source




回答2:


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}




回答3:


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.



来源:https://stackoverflow.com/questions/16252399/how-do-i-add-a-final-variable-to-class-diagram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!