'How to model python properties in UML diagram
What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like instance attributes. So, how would you suggest to present that in my class diagram?
Solution 1:[1]
I recommend you read UML Best Practice: Attribute or Association, by another Stack Overflow user named Geert Bellekens. It states simply:
Use Associations for Classes and Attributes for DataTypes.
You should write the Python attributes that are typed by non-datatypes (which have identity) at the ends of UML associations connecting to those UML classes. You should write the Python attributes typed by simple datatypes (which have no identity other than their value) in the UML class' attribute box.
The accessors and mutators are largely just noise. A model compiler or IDE can generate those for you.
Solution 2:[2]
Good practice is what works on your project.
In order to model Python properties you can add stereotyped getter and setter operations which indicate their use. The link between attribute and operation is usually done via a naming convention. Some tools offer internal linkage to make attributes properties with getters and setters.
If you are not using code generation you can also stereotype the attribute to indicate their use as properties (thus telling the coder to use @property
) and leave away the operations. If you are using your own code generator this would work analogously. Tool embedded code generators might need the additional operations as described above.
Solution 3:[3]
It really depends what kind of template your UML tool uses. In some tools there is a Properties box along the common Attributes and Methods boxes. The UML notation states that attributes are written in lower camelcase, you could write properties in upper camelcase. They would also differ visually because of the public access modifier (+).
Do you have the need to specify different access modifiers for the getter and setter? Im not sure how i would go about that. Keep in mind the level of abstraction necessary.
Remember that UML is mainly a set of defined standards. If the standard needs a slight tweak to fit your needs dont hesitate. The important thing is that your team and stakeholders understand the syntax.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | qwerty_so |
Solution 3 | LuqJensen |