AlgoViz

UML class diagrams

easy 45 min

A 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

0:00 / 4:49

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

strangerany classmay use+childPupil is a Personmay use+ #ashaPerson+ nameAsha# id17- age11

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
courseyes: rules, changesclass 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

    minor

    Filled is only for the composition diamond. A filled triangle makes the reader wonder which line it is.

    Wrong
    Dog ──▶ Animal     (filled)
    Right
    Dog ──▷ Animal     (hollow)
  • Giving the inner list away

    critical

    The filled diamond says that the whole decides about its parts. Anybody with the list decides too.

    Wrong
    def rooms(self):    return self._rooms
    Right
    def rooms(self):    return tuple(self._rooms)
  • A number without a check

    critical

    1..* in the picture and an empty list in the program. The crash comes far away from the cause.

    Wrong
    def __init__(self, topics):    self._lessons = [Lesson(t) for t in topics]
    Right
    def __init__(self, topics):    if not topics: raise ValueError("at least one lesson")    self._lessons = [Lesson(t) for t in topics]
  • Needs drawn as owns

    major

    An invoice needs an address, and others use the same one. A cascade derived from the diamond deletes it for all.

    Wrong
    Invoice *-- Address
    Right
    Invoice --> Address      (shared)Invoice with a copied address value   (better)
  • Two ends, two doors

    major

    Each side changes its own list, and one day they disagree.

    Wrong
    course.students.append(s)s.courses.append(course)     # sometimes forgotten
    Right
    course.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