Class Diagram and Code in Sync
About this app
Python code of the running log on the left, the UML class diagram in exact notation on the right: point at a code line and its counterpart in the diagram lights up, and vice versa. self.laeufe = [] does not become an attribute but the line with the star; the hollow triangle sits at the base class. Eight stages from class Lauf to staff administration, plus part and whole: on deletion the object stage shows that composition and aggregation are not in the code but in the meaning.
From code to class diagram
A UML class diagram shows a program’s classes as boxes with three compartments: name, attributes, methods. Translated from Python: class Lauf becomes the box Lauf, self.distanz = d becomes the attribute distanz: float, and def tempo(self) becomes tempo(): float, without self. A leading underscore becomes “−” (private), otherwise “+” (public). A list such as self.laeufe = [] is not an attribute but an association with multiplicity *. Inheritance such as class Wettkampf(Lauf) is drawn as a line with a hollow triangle at the base class.
What the app shows
On the left is the Python code of a running log, on the right the UML class diagram in exact notation. Point at a line of code and its counterpart in the diagram lights up, and vice versa; for each line a note explains why it translates as it does, or why it has no counterpart, such as the constructor. Eight stages lead from class Lauf to a staff management example. The code can be run step by step, with a console and an object stage showing the objects as they are created.
Part and whole
Three exercises cover composition, aggregation and a cross-check. The code starts hidden: first think about how a line such as Serie * o--> * Wettkampf looks in Python, then reveal it. When a whole is deleted, the object stage shows what happens to its parts. This makes visible that composition and aggregation often look the same in Python code and differ mainly in meaning: whether the parts outlive the whole.
Frequently asked questions
- How do you draw a UML class diagram from Python code?
- Each class becomes a box with name, attributes and methods. Attributes come from assignments to self, methods from def lines without the self parameter. Lists of objects of other classes become associations with multiplicity, and base classes are linked by inheritance arrows with a hollow triangle.
- What is the difference between composition and aggregation?
- Both describe a part–whole relationship. In composition (filled diamond) the parts cannot exist without the whole and are deleted with it. In aggregation (hollow diamond) the parts can exist independently and may belong to several wholes.
- What do + and − mean in a class diagram?
- + marks public attributes and methods that belong to the class interface. − marks private elements used only inside the class. In Python this corresponds by convention to a name without or with a leading underscore.
- How is the app structured?
- Eight stages build up the running log step by step, plus three exercises on composition, aggregation and a cross-check. Code and diagram are linked, and the code can be run step by step with a console and object stage.
Subject: Software engineering
More from Software engineering
- Tests as a Safety Net
- Token Game in Activity Diagrams
- Trust, but Verify
- Waterfall or Agile?
- WIP Limit and Lead Time
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps