A diagram says what pages of words cannot
A model is a simplified picture of a system that shows its parts and how they connect. A model lets a team agree on the parts before anyone writes code.
A diagram is the most common way to draw one. It is far cheaper to move a box on a diagram than to rewrite a program.
By the end of this lesson
Note
I can draw a model of a system with a class diagram and a sequence diagram.
Key words
| Word | What it means | Example |
|---|---|---|
| Model | a simplified picture of a system | |
| Entity | a thing the system keeps facts about | Room |
| Attribute | one fact about an entity | hours |
| Relationship | a link between two entities | |
| Class diagram | a picture of entities, their attributes and links | |
| Sequence diagram | a picture of messages in time order |
The cells students fill in are left blank in this preview.
Entities are things a system keeps facts about
Read the requirements and underline the nouns. A noun the system keeps facts about is an entity. Each fact about it is an attribute.
In the booking system the entities are Person, Room and Booking. A booking has a slot, hours and a status. A link between two entities is a relationship: a person makes a booking, and a room holds one.
How to read the class diagram
Each box is an entity. The lines under a name are its attributes. An arrow is a relationship, and the word beside it says what the link means.
Here Person and Room each point at Booking. A booking sits in the middle and is linked to one person and one room.
A model can be ahead of the database
The model names Room as an entity. The lab's table does not have a rooms table. It keeps the room as plain text on every booking.
Run this to list the distinct rooms the bookings mention. It prints Lab 2 and Lab 4, one per line. Room is real in the model, and only repeated text in the table.
Give psql the password
export PGPASSWORD=shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make the bookings table
psql -h db -U shop -c "drop table if exists bookings; create table bookings (id serial primary key, room text not null, slot text not null, booked_by text not null, hours int not null, status text not null default 'booked'); insert into bookings (room, slot, booked_by, hours) values ('Lab 2', 'Mon 10:00', 'Asha', 2), ('Lab 4', 'Mon 10:00', 'Ben', 1), ('Lab 2', 'Tue 14:00', 'Asha', 3); notify pgrst, 'reload schema'"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
List the rooms the bookings name
psql -h db -U shop -At -c "select distinct room from bookings order by room"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think a model must match the database table by table. A model shows ideas the users share. A database may store them differently. Draw the model first from the requirements, then decide how to store it.
A sequence diagram shows messages in order
A class diagram shows what exists. A sequence diagram shows what happens, from top to bottom in time.
Each column is a part of the system. Each arrow is a message. A dashed arrow is an answer coming back.