Arrows between classes
So far each box stood alone. In a program, objects hold references to other objects, and a class diagram shows that with lines between the boxes.
A field that refers to another class
A theater stages plays, and each play gets many performances. The program has a class for each, and a Performance holds a reference to its Play:
public class Performance {
private Play play;
private String date;
private int seatsSold;
// the constructor and methods
}
In a diagram, a field whose type is another class of the diagram isn't written in the box. It's drawn as an arrow, from the box of the class that has the field to the box of the class it refers to:
The diagram as text
- Play, a class
- private field
title: String - private field
minutes: int - public constructor
Play(title: String, minutes: int) - public method
getTitle(): String - public method
getMinutes(): int
- private field
- Performance, a class
- private field
date: String - private field
seatsSold: int - public constructor
Performance(play: Play, date: String) - public method
sell(seats: int): void - public method
toString(): String
- private field
Connections:
- Each Performance has one Play object (play).
- The arrow goes from Performance to Play: a Performance knows its Play. It has a field of type Play, so its methods can call the play's methods.
- A Play doesn't know its performances. It has no field for them, and the arrow doesn't point back: given a Play object, there's no way to reach its performances.
- The
1at the arrowhead is the multiplicity: each Performance has exactly one Play. An arrow without a number at its head means one, too. - The name by the arrow,
play, is the field's name. It's optional, and handy when a class has two fields of the same type. play: Playin the constructor is a parameter, so it stays in the box. Only the field turns into an arrow.date: Stringstays in the box, though String is a class too. Arrows connect the program's own classes, the ones drawn in the diagram. Java's classes, such as String and ArrayList, are written as types.
Here's the code:
public class Main {
public static void main(String[] args) {
Play hamlet = new Play("Hamlet", 190);
Performance friday = new Performance(hamlet, "May 9");
Performance saturday = new Performance(hamlet, "May 10");
friday.sell(120);
saturday.sell(95);
friday.sell(30);
System.out.println(friday);
System.out.println(saturday);
}
}public class Play {
private String title;
private int minutes;
public Play(String title, int minutes) {
this.title = title;
this.minutes = minutes;
}
public String getTitle() {
return this.title;
}
public int getMinutes() {
return this.minutes;
}
}public class Performance {
private Play play;
private String date;
private int seatsSold;
public Performance(Play play, String date) {
this.play = play;
this.date = date;
this.seatsSold = 0;
}
public void sell(int seats) {
this.seatsSold += seats;
}
@Override
public String toString() {
return this.play.getTitle() + " (" + this.play.getMinutes() + " min), " + this.date + ": " + this.seatsSold + " seats sold";
}
}Hamlet (190 min), May 9: 150 seats sold
Hamlet (190 min), May 10: 95 seats soldBoth performances refer to the same Play object. "Each Performance has one Play" says how many plays a performance has, not that their plays are different objects: many performances can share one play, as friday and saturday do.
One, or none
A reference can also be null. A multiplicity of 0..1 at the arrowhead means at most one: zero or one. Say a critic may review a performance, or not:
The diagram as text
- Performance, a class
- private field
date: String - private field
seatsSold: int
- private field
- Review, a class
- private field
critic: String - private field
stars: int
- private field
Connections:
- Each Performance has at most one Review object (review).
Performance's field review is null until a review comes in, so code that uses it has to check for null first. With 1, the code can count on the field always referring to an object.