System Design Academy

Saving…

Lesson 8 of 10 in Module 10 of 45 lessons complete

Object Relationships

Module 1, lesson 8Concept12 min

Definition

Association

In plain English
Association means two types know about each other and interact, but neither owns the other.
Think of it like
It is like a doctor and a patient who work together but live separate lives.

Aggregation

In plain English
Aggregation means one type groups others that can still exist on their own.
Think of it like
It is like a playlist and its songs, where deleting the playlist keeps the songs.

Composition (relationship)

In plain English
Composition, as a relationship, means the part cannot exist without its whole.
Think of it like
It is like a building and its rooms, which disappear when the building is demolished.

Dependency

In plain English
A dependency means one type briefly uses another, usually as a function parameter, without keeping it.
Think of it like
It is like borrowing a ladder for one job and returning it.

Why it exists

A ward closes. Its beds belong to it, but its nurses remain on the hospital roster. Deleting both would remove nurses still working elsewhere.

Decide who owns each part before choosing fields. A pointer or slice does not say who creates or removes it.

How it works

Multiplicity

In plain English
Multiplicity says how many values of one type relate to another, such as one-to-many.
Think of it like
It is like one teacher with many students.
RelationshipUML arrowGo shapeLifetime rule
Association-->*Doctor fieldKeeps a link to a value owned elsewhere
Aggregationo--[]*Song supplied by callerGroups parts that also exist on their own
Composition*--Private value fields or internally created slicesCreates and removes parts as part of the whole
Dependency..>Method parameterUses a value briefly without keeping it

Ward owns beds and groups independent nurses. A nurse keeps a patient link and temporarily uses a Thermometer interface with a Read method.

Four relationships appear in one ward model.

Ward owns beds and groups independent nurses. A nurse keeps a patient link and temporarily uses a Thermometer interface with a Read method.

Ward owns beds, groups nurses, and nurses borrow thermometers.
Go
package main

import "fmt"

type Bed struct{ number int }
type Patient struct{ Name string }
type Thermometer interface{ Read() float64 }
type Nurse struct{ Patient *Patient }

func (n Nurse) TakeReading(t Thermometer) float64 { return t.Read() }

type Ward struct {
	beds   []Bed
	nurses []*Nurse
}

func NewWard(nurses []*Nurse) *Ward {
	return &Ward{beds: []Bed{{1}, {2}}, nurses: append([]*Nurse(nil), nurses...)}
}

type DigitalThermometer struct{}

func (DigitalThermometer) Read() float64 { return 37.2 }
func main() {
	patient := &Patient{Name: "Asha"}
	nurse := &Nurse{Patient: patient}
	ward := NewWard([]*Nurse{nurse})
	fmt.Println(len(ward.beds), nurse.Patient.Name, nurse.TakeReading(DigitalThermometer{}))
}

Examples

Who stays when the ward closes?

Ward creates its own beds. Nurses are assigned from the hospital roster and keep links to their patients.

Ward owns beds, groups nurses, and nurses borrow thermometers.
Go
package main

import "fmt"

type Bed struct{ number int }
type Patient struct{ Name string }
type Thermometer interface{ Read() float64 }
type Nurse struct{ Patient *Patient }

func (n Nurse) TakeReading(t Thermometer) float64 { return t.Read() }

type Ward struct {
	beds   []Bed
	nurses []*Nurse
}

func NewWard(nurses []*Nurse) *Ward {
	return &Ward{beds: []Bed{{1}, {2}}, nurses: append([]*Nurse(nil), nurses...)}
}

type DigitalThermometer struct{}

func (DigitalThermometer) Read() float64 { return 37.2 }
func main() {
	patient := &Patient{Name: "Asha"}
	nurse := &Nurse{Patient: patient}
	ward := NewWard([]*Nurse{nurse})
	fmt.Println(len(ward.beds), nurse.Patient.Name, nurse.TakeReading(DigitalThermometer{}))
}

TakeReading uses the thermometer during one call. Closing the ward leaves nurses and patients on the hospital roster.

The insight

Ask whether the part still exists when its group disappears.

Trade-offs

Pros

  • Ownership makes creation and removal rules explicit.
  • Private parts are easier to protect with checked methods.

Cons

  • An exclusively owned part cannot also belong to another whole.
  • Mistaken ownership can remove things still needed elsewhere.

Use when

  • A part has no independent meaning in the model.
  • The whole must maintain the part’s rules.

Avoid when

  • Several wholes share the same part.
  • The part must outlive its current group.

Quick check

A Tour lists Exhibits, and the same exhibits appear in other tours. Relationship?
Courier.Deliver(p *Parcel, s Scanner) uses the scanner only during the call. Relationship to Scanner?

Takeaways

  • Association links, aggregation groups, composition owns, dependency borrows.
  • Ask who owns each part and when it stops existing.
  • Temporary use leaves fewer lasting connections between types.

Try the quick check above whenever you feel ready.

Previous: Composition vs Inheritance