Object Relationships
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.
| Relationship | UML arrow | Go shape | Lifetime rule |
|---|---|---|---|
| Association | --> | *Doctor field | Keeps a link to a value owned elsewhere |
| Aggregation | o-- | []*Song supplied by caller | Groups parts that also exist on their own |
| Composition | *-- | Private value fields or internally created slices | Creates and removes parts as part of the whole |
| Dependency | ..> | Method parameter | Uses 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.
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.
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.
Shared exhibits, owned audio
A Tour lists exhibits shared with other tours. Each Exhibit owns its AudioTrack in this model.
Tour groups shared Exhibit values. Each Exhibit owns its AudioTrack.
Deleting a tour removes its list, not the exhibits. Removing an exhibit also retires its private audio track.
The insight
Aggregation shares parts; composition owns parts that end with their whole.
A courier borrows a scanner
LockerBank creates its lockers. A parcel keeps a link to its assigned locker, while a delivery call briefly uses a scanner.
package main
import "fmt"
type Locker struct{ occupied bool }
type LockerBank struct{ lockers []Locker }
func NewLockerBank() *LockerBank { return &LockerBank{lockers: make([]Locker, 2)} }
type Parcel struct {
ID string
Locker *Locker
}
type Scanner interface{ Scan(id string) error }
type Courier struct{}
func (Courier) Deliver(p *Parcel, s Scanner) error { return s.Scan(p.ID) }
type HandScanner struct{}
func (HandScanner) Scan(id string) error { fmt.Println("scanned", id); return nil }
func main() {
bank := NewLockerBank()
p := &Parcel{ID: "P7", Locker: &bank.lockers[0]}
fmt.Println((Courier{}).Deliver(p, HandScanner{}))
}
Inside this package, a parcel keeps a locker link. Other packages should use bank methods or locker IDs to protect the bank's rules.
The insight
Using a scanner for one call creates no lasting link.
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
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.