I've been using <<get>>
and <<set>>
stereotypes next to property names so they look like fields, yet allows you to differentiate the access modifiers for the get
or set
:
+=============================+
| ClassName |
+-----------------------------+
| +<<get>> Id : int |
| -<<set>> Id : int |
| +<<get>> IsSomething : bool |
+-----------------------------+
| + Method1(arg1 : string) |
+=============================+
Alternatively, if you don't want more than one occurrence of a property, this could work as well:
+=============================+
| ClassName |
+-----------------------------+
| +<<get>> -<<set>> Id : int |
And to reduce clutter, if the get
and set
have the same access modifier:
+====================================+
| ClassName |
+------------------------------------+
| +<<get, set>> Description : string |
| +<<get>> -<<set>> Id : int |
This clearly communicates whether a property has a get or set, and if it is readonly (by way of no <<set>>
existing in the class diagram). So basically what you said in your question.
While properties are syntactic sugar for getter and setter methods, they are supposed to feel like fields, and I believe the UML diagram should reflect that fact, and at the same time also communicate what is public and what is private, and also whether a setter exists or not.
Update: I've been using this notation for a while, and came up with a few slight modifications.
Read-only property
+<<get>> IsVisible : bool
Having a get with no set implies:
public bool IsVisible => // logic goes here
Auto-property with read-only backing field
+<<get>> Id : int {readonly}
Implies:
public int Id { get; }