Arrows for "has a" and "uses"
After this: Tell composition, aggregation, association and a use apart, and read how many from the labels.
On this page · 6 sections · 4 checks
A school has classrooms. A classroom has students, and it has a teacher. All three are "has a", but they are not the same. If the school closes a classroom, that classroom is gone. The students are not: they existed before they joined the class, and they go home afterwards. A good diagram shows that difference, and this lesson shows how.
Owns, or only holds
Ask two questions about any "has a":
- Who makes the part? The whole, inside its own method, or someone else, who hands it in?
- Does the part live on without the whole? Could another object hold it too?
When the whole makes the part and the part belongs to that whole alone, the link is , drawn as a filled diamond at the owner. Say it as "owns, and makes". When the part is made elsewhere and handed in, so it can exist before and after, the link is , drawn as a hollow diamond at the holder. Say it as "holds, but does not own".
In the school program that lesson 4 builds, the school makes each classroom itself, in a method that opens a room. Nobody hands it a classroom. The students are made first and then added to a classroom. Press Run it to watch the school diagram grow one line at a time.
- Student inherits from Person
- Teacher inherits from Person
Press Run it to build the diagram one step at a time.
- classes
- 3
- relations
- 2
The diamond always sits at the whole: at School for its classrooms, at Classroom for its students. The line itself is solid for both.
Knows about, and needs for a moment
The classroom also keeps one teacher in a field. The teacher is not a part of the room. The classroom only knows about its teacher. That link is an : a solid line, with an open arrowhead pointing at the class that is known. Some drawings leave the arrowhead off. Say it as "knows about".
The sixth kind is the lightest. Sometimes a class needs another one only during one method call: it takes it as an input, or makes it in a local variable, and keeps nothing afterwards. That link is a , drawn as a dashed arrow. Say it as "needs for a moment". Here a report card prints one student's details and keeps no student afterwards.
- ReportCard uses Student
How many
A small label at the end of a line says how many objects of that class there are, for one object at the other end. This count is called .
| Label | Read it as |
|---|---|
1 | exactly one |
0..1 | none or one |
0..* | zero or more |
1..* | one or more |
In the school, 1 at the School end and 0..* at the Classroom end say: one school has zero or more classrooms, and each classroom belongs to exactly one school. 0..* at Student says a classroom can have any number of students, even none. 1 at Teacher says each classroom has exactly one teacher.
All six kinds
| Kind | Say it as | Drawn as |
|---|---|---|
| inheritance | is a | solid line, hollow triangle at the parent |
| interface | can do | dashed line, hollow triangle at the interface |
| composition | owns, and makes | solid line, filled diamond at the owner |
| aggregation | holds, but does not own | solid line, hollow diamond at the holder |
| association | knows about | solid line, open arrowhead |
| dependency | needs for a moment | dashed line, open arrowhead |
Check yourself
The school makes each classroom itself, inside open_roomopenRoom, and keeps it in its list. Which kind of line joins School and Classroom?
Two new pairs. A Playlist holds songs that already exist in the music library, and one song can be on many playlists. An Order makes its own OrderLine objects as items are added, and each order line belongs to that one order. Which diamonds?
The line from Classroom to Student has the label 0..* at the Student end. What does it say?
A Printer class has one method, print(doc: Document), and keeps no Document in any field. Which line joins Printer and Document?
Where the line is blurry
- Choose between the two diamonds with two questions: who makes the part, and does it live on without the whole?
- Draw composition as a filled diamond at the owner, and aggregation as a hollow diamond at the holder.
- Draw association, knows about, as a solid line with an open arrowhead, and a dependency, needs for a moment, as a dashed arrow.
- Read a multiplicity label: 1 is exactly one, 0..1 is none or one, 0..* is zero or more, 1..* is one or more, counted at the class the label sits next to.