35

In a UML class diagram, if a class has 5 private attributes that need to be mutable and readable, the UML gets pretty ugly with 10 get/set methods even without any of the class' interesting functionality:

Bloated

Ugliness aside, I feel like the UML should focus on the class' more interesting functionality. Am I correct?

Is there some standard shortcut for denoting or implying getters and setters for private attributes?

observer
  • 2,925
  • 1
  • 19
  • 38
kdbanman
  • 10,161
  • 10
  • 46
  • 78
  • 1
    If you don't want to model the not-interesting functionality then you can just get rid of the getters/setters altogether and model the properties as simple attributes. Fill only the upper attributes compartment and leave the operations compartment empty. In some languages (e.g. `C#`, `Delphi`) the getters/setters are almost invisible to the programmer anyway. I feel that you should not transfer any responsibility to _UML_ ("_Object Management Group (OMG) is a not-for-profit technology standards consortium..._") and focus on the responsibility at your side of the keyboard. Am I correct? – xmojmr Jan 25 '15 at 18:58

2 Answers2

45

You are correct: there is no need to include the (noise of) "boilerplate" signatures of standard setters and getters in a class model. Unfortunately, UML does not define a standard notation for implying getters and setters for private attributes. So, you'd have to use your own convention. For instance, you could include a general explanation (that all private properties have getters and setters, while private read-only properties have only getters) as a UML Comment, shown as a rectangle with the upper right corner bent (also called a “note symbol”) attached to the diagram.

If you prefer to make the getter/setter convention more explicit for the properties concerned, then create your own stereotypes (e.g., «get/set» and «get») to be used for categorizing these private properties, as shown in the following diagram:

Class with get/set attribute stereotypes

I'm also using this for describing/documenting the implicit getters and setters of ECMAScript 6 classes.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • nice idea, that should be part of uml 3.0(?) :) – Martin Pfeffer Jan 31 '17 at 19:55
  • Great idea, still think it can be improved by agreeing on an even simpler symbol or notation for `readable` and `mutable` this is quite explicit and readable but a bit verbose for my taste – LeedMx Jan 02 '19 at 19:28
5

UML does not define getter setter operations. Get and Set method are used in programming languages to realize attribute definition. For example, readonly attribute will have getter method only in implementation code. if attribute is defined as calculated, getter method is usually used in code to implement calculation, and setter can be leaved out, because calculated attributes are usually readonly. Visibility of attribute is usually moved to visibility to getter and setter methods in code as well. It does not make sense to define geter and setter methods in code for attributes defined read write and not calculated.

Vladimir
  • 2,066
  • 15
  • 13