How Many, and Who Owns It: Multiplicity and the Two Diamonds
An arrow says that a rental has a rentable, but not how many or who owns it; numbers on the line (1, 0..1, * and 1..*) say how many, and a hollow or filled diamond says whether the part can live without the whole.
Apprentice · Software Development · OOP · Updated
After this lesson you can
- Read the numbers on a line (1, 0..1, *, 1..*) and say what they allow
- Tell aggregation from composition by asking whether the part can live without the whole
Key takeaways
- The numbers on a line (its multiplicity) say how many objects are at that end: 1, 0..1, * (any number, none included) or 1..* (one or more).
- Read a line from one class, and the number at the far end: a rental has 1..* rentables, and a rentable is on 0..1 rentals at a time.
- A hollow diamond (aggregation) marks a whole whose parts live on without it, like a team and its players.
- A filled diamond (composition) marks a whole that makes and owns its parts, which go when it goes, like a hand and its fingers.
Before this: Arrows: Is-a, Has-a and Uses.
On Saturday morning Leo's family comes to the desk: two bikes and an e-bike, for two hours, on one receipt. The desk can't do it. A Rental holds one rentable, so the clerk would have to make three rentals and staple three receipts together.
Look at the class diagram from the last lesson and the problem was hiding in plain sight. The line from Rental to Rentable said "a rental has a rentable", and nobody asked: how many? One? Several? Could it be none? The arrow can't say. And the bikes Leo takes: do they belong to the rental, or to the desk's rack? This lesson adds the two things a line needs to answer those questions: numbers, and diamonds.
A rental with one or more rentables
First, the fix in code: a rental now holds a list of items, and its total adds up each one's price. The bikes come off the desk's rack:
List<IRentable> rack = new List<IRentable> { new Bike("4821"), new Bike("5532"), new EBike("7310"), new Scooter() };
Rental rental = new Rental("Leo", new List<IRentable> { rack[0], rack[1], rack[2] }, 2);
Console.WriteLine( quot;{rental.Rider}: {string.Join(", ", rental.Items.Select(item => item.Name))}, {rental.Hours}h, ${rental.Total()}");
Console.WriteLine( quot;On the rack: {rack.Count}");
interface IRentable
{
string Name { get; }
int Price(int hours);
}
class CodeLock
{
private string code;
public CodeLock(string code) { this.code = code; }
}
class Bike : IRentable
{
private CodeLock codeLock;
public Bike(string lockCode) { codeLock = new CodeLock(lockCode); }
public virtual string Name => "Bike";
public virtual int Price(int hours) => 10 * hours;
}
class EBike : Bike
{
public EBike(string lockCode) : base(lockCode) { }
public override string Name => "E-bike";
public override int Price(int hours) => 15 * hours;
}
class Scooter : IRentable
{
public string Name => "Scooter";
public int Price(int hours) => 8 * hours;
}
class Rental
{
public string Rider { get; }
public List<IRentable> Items { get; }
public int Hours { get; }
public Rental(string rider, List<IRentable> items, int hours)
{
Rider = rider;
Items = items;
Hours = hours;
}
public int Total() => Items.Sum(item => item.Price(Hours));
}
Leo: Bike, Bike, E-bike, 2h, $70
On the rack: 4
interface Rentable {
String name();
int price(int hours);
}
class CodeLock {
private String code;
CodeLock(String code) { this.code = code; }
}
class Bike implements Rentable {
private CodeLock lock;
Bike(String lockCode) { lock = new CodeLock(lockCode); }
public String name() { return "Bike"; }
public int price(int hours) { return 10 * hours; }
}
class EBike extends Bike {
EBike(String lockCode) { super(lockCode); }
public String name() { return "E-bike"; }
public int price(int hours) { return 15 * hours; }
}
class Scooter implements Rentable {
public String name() { return "Scooter"; }
public int price(int hours) { return 8 * hours; }
}
class Rental {
String rider;
List<Rentable> items;
int hours;
Rental(String rider, List<Rentable> items, int hours) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
public int total() {
int total = 0;
for (Rentable item : items) {
total += item.price(hours);
}
return total;
}
}
void main() {
List<Rentable> rack = List.of(new Bike("4821"), new Bike("5532"), new EBike("7310"), new Scooter());
Rental rental = new Rental("Leo", List.of(rack.get(0), rack.get(1), rack.get(2)), 2);
List<String> names = rental.items.stream().map(Rentable::name).toList();
IO.println(rental.rider + ": " + String.join(", ", names) + ", " + rental.hours + "h, quot; + rental.total());
IO.println("On the rack: " + rack.size());
}
Leo: Bike, Bike, E-bike, 2h, $70
On the rack: 4
class CodeLock:
def __init__(self, code): self._code = code
class Bike:
name = "Bike"
def __init__(self, lock_code): self._lock = CodeLock(lock_code)
def price(self, hours): return 10 * hours
class EBike(Bike):
name = "E-bike"
def price(self, hours): return 15 * hours
class Scooter:
name = "Scooter"
def price(self, hours): return 8 * hours
class Rental:
def __init__(self, rider, items, hours):
self.rider = rider
self.items = items
self.hours = hours
def total(self):
return sum(item.price(self.hours) for item in self.items)
rack = [Bike("4821"), Bike("5532"), EBike("7310"), Scooter()]
rental = Rental("Leo", [rack[0], rack[1], rack[2]], 2)
names = ", ".join(item.name for item in rental.items)
print(f"{rental.rider}: {names}, {rental.hours}h, ${rental.total()}")
print(f"On the rack: {len(rack)}")
Leo: Bike, Bike, E-bike, 2h, $70
On the rack: 4
class CodeLock {
#code;
constructor(code) { this.#code = code; }
}
class Bike {
name = "Bike";
#lock;
constructor(lockCode) { this.#lock = new CodeLock(lockCode); }
price(hours) { return 10 * hours; }
}
class EBike extends Bike {
name = "E-bike";
price(hours) { return 15 * hours; }
}
class Scooter {
name = "Scooter";
price(hours) { return 8 * hours; }
}
class Rental {
constructor(rider, items, hours) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total() {
let total = 0;
for (const item of this.items) {
total += item.price(this.hours);
}
return total;
}
}
const rack = [new Bike("4821"), new Bike("5532"), new EBike("7310"), new Scooter()];
const rental = new Rental("Leo", [rack[0], rack[1], rack[2]], 2);
const names = rental.items.map((item) => item.name).join(", ");
console.log(`${rental.rider}: ${names}, ${rental.hours}h, ${rental.total()}`);
console.log(`On the rack: ${rack.length}`);
Leo: Bike, Bike, E-bike, 2h, $70
On the rack: 4
interface Rentable {
name: string;
price(hours: number): number;
}
class CodeLock {
private code: string;
constructor(code: string) { this.code = code; }
}
class Bike implements Rentable {
name = "Bike";
private lock: CodeLock;
constructor(lockCode: string) { this.lock = new CodeLock(lockCode); }
price(hours: number): number { return 10 * hours; }
}
class EBike extends Bike {
name = "E-bike";
price(hours: number): number { return 15 * hours; }
}
class Scooter implements Rentable {
name = "Scooter";
price(hours: number): number { return 8 * hours; }
}
class Rental {
rider: string;
items: Rentable[];
hours: number;
constructor(rider: string, items: Rentable[], hours: number) {
this.rider = rider;
this.items = items;
this.hours = hours;
}
total(): number {
let total = 0;
for (const item of this.items) {
total += item.price(this.hours);
}
return total;
}
}
const rack: Rentable[] = [new Bike("4821"), new Bike("5532"), new EBike("7310"), new Scooter()];
const rental = new Rental("Leo", [rack[0], rack[1], rack[2]], 2);
const names = rental.items.map((item) => item.name).join(", ");
console.log(`${rental.rider}: ${names}, ${rental.hours}h, ${rental.total()}`);
console.log(`On the rack: ${rack.length}`);
Leo: Bike, Bike, E-bike, 2h, $70
On the rack: 4
Two bikes at $10 an hour and an e-bike at $15, for two hours: $20 + $20 + $30 = $70. Python's tab leaves out the abstract Rentable class this time, to keep it short; the code works the same with it.
Numbers on the line
The new rule, "a rental has one or more rentables", goes on the diagram as a number at the end of the line. That number is the line's multiplicity: how many objects can be at that end. Four of them cover nearly every diagram you'll read:
| On the line | Read it as | At the desk |
|---|---|---|
1 |
exactly one | a bike has exactly one lock |
0..1 |
none or one | a rentable is on none or one rental at a time |
* |
any number, none included (also written 0..*) |
the desk has any number of rentals today: at 8 a.m., none |
1..* |
one or more | a rental has one or more rentables |
The two dots are read "to": 0..1 is "zero to one", 1..* is "one to many". The star is read "many".
To read a line, start at one class and read the number at the far end. From Rental: a rental has 1..* rentables. Then turn around and read the other end from Rentable: a rentable is on 0..1 rentals, because at any moment a bike is either on the rack or out with someone, never on two rentals at once. One line, two sentences, both of them rules the code must keep.

Leaving a number off doesn't mean "one"; it means "not said". When the count matters to the design, write it. The 1..* is the one that would have caught Leo's family before they reached the desk.
Who owns the part? The two diamonds
There's one more question the line can answer. When Leo's rental is over, his bikes go back on the rack: they were there before the rental and they're still there after it. That's what the last line of the program shows: four on the rack, the whole time. But a bike's lock is different. The bike makes its own lock (look for new CodeLock inside Bike's constructor) and never hands it to anyone. If the bike is scrapped, its lock goes with it.
An everyday pair shows the difference. A team and its players: a player can leave the team, join another one, and is still a player. A hand and its fingers: the fingers don't go off and join another hand.

A class diagram draws both as a diamond on the line, at the whole's end:
| Diamond | Name | Means | At the desk |
|---|---|---|---|
| hollow ◇ | aggregation | the whole holds parts that live on without it | a rental holds rentables from the rack |
| filled ◆ | composition | the whole makes and owns its parts; they go when it goes | a bike owns its lock |

To tell which one you have, ask two questions about the part: who made it, and could it live without the whole? If the whole says new for the part inside itself and never hands it out, it's composition. If the part was handed in from outside, it's aggregation.
Many teams draw a plain has arrow and skip the diamonds, and that's fine. Draw a diamond when ownership changes the design: when the question is who cleans up, what gets copied along with the whole, or what's lost if the whole goes away.
Try it. The desk adds a Repair record: each repair is written for one bike, and the bike keeps its repairs in a list for its whole life. Which diamond, at which end, and which number at the Repair end? (A filled diamond at the Bike end, with * at the Repair end: a bike owns any number of repair records, which go if the bike's record does.)
Check yourself
2 questions to try on the lesson page.
So what
A number turns "has a" into a rule the code can be checked against, and a diamond says who's in charge of the part. Next, the same desk as real objects, at one moment on a Saturday.