UML class diagrams
easy 45 minA class diagram is a picture of the types in a program and the lines between them.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
What each mark and line promises, seen on living objects.
Operations
Locks at run time
A stranger and a subclass try the members of a person.
Step 1 of 5
One object
Asha has three fields with three marks. The compartments of the box became values.
Step 1: One object
class Person: def __init__(self, name, age): self.name = name # + public self._id = 17 # # protected, by agreement self.__age = age # - private def birthday(self): # + public self.__age += 1- Good for
- Protecting rules
- Costs
- Methods to ask through
Python only renames __age. The lock is an agreement with a good memory help.
In the real world
The same uml class diagrams doing real jobs. Pick one and step through it.
Which nouns of the music school become classes?
Step 1 of 6
Course
| Own rules or life? | Becomes | |
|---|---|---|
| course | yes: rules, changes | class Course |
| lesson | ||
| student | ||
| level | ||
| seats | ||
| message |
It has rules about seats and changes over time.
Step 1: Course
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Filled triangles
minorFilled is only for the composition diamond. A filled triangle makes the reader wonder which line it is.
WrongDog ──▶ Animal (filled)RightDog ──▷ Animal (hollow)Giving the inner list away
criticalThe filled diamond says that the whole decides about its parts. Anybody with the list decides too.
Wrongdef rooms(self): return self._roomsRightdef rooms(self): return tuple(self._rooms)A number without a check
critical1..* in the picture and an empty list in the program. The crash comes far away from the cause.
Wrongdef __init__(self, topics): self._lessons = [Lesson(t) for t in topics]Rightdef __init__(self, topics): if not topics: raise ValueError("at least one lesson") self._lessons = [Lesson(t) for t in topics]Needs drawn as owns
majorAn invoice needs an address, and others use the same one. A cascade derived from the diamond deletes it for all.
WrongInvoice *-- AddressRightInvoice --> Address (shared)Invoice with a copied address value (better)Two ends, two doors
majorEach side changes its own list, and one day they disagree.
Wrongcourse.students.append(s)s.courses.append(course) # sometimes forgottenRightcourse.enrol(s) # changes both ends
Interview tips
Draw in passes
- Names
- Lines
- Numbers
- Few members
Say each line
- From, verb, to, number
- Course owns one or more lessons
- No sentence, no line
Leave out
- Getters
- Constructors
- Library types
- Boxes that answer no question
Change the picture
- Time: sequence diagram
- States: state diagram
- Servers: architecture