CSE Roadmap

Common tier · about 2 hours · 7 parts

Part 2 of 7 · 1 min read

Two ideas from GoF

Owning versus knowing.

A person owns a wallet and knows a friend. The wallet goes where the person goes. The friend has a life of their own. GoF draws the same line between objects.

Aggregation (GoF's word) means one object owns, or is responsible for, another. The owner can be said to contain the other object as a part of itself. GoF adds that an aggregate and its owner have identical lifetimes: they are created and end together. Acquaintance means an object merely knows of another. Other names for it are association and the "using" relationship. Two acquainted objects can call on each other, but neither is responsible for the other. GoF says acquaintance is a weaker link, and suggests much looser coupling.

GoF is careful about one thing: the two are easy to confuse, because they are often written in code the same way, as a reference held in a field. The difference is in intent, not in syntax. Aggregation links tend to be fewer and more permanent. Acquaintances are made and remade often, and sometimes last only for one operation, so they are harder to see in the source.

Aggregation and acquaintance (GoF)

Aggregation means an object owns another and both live and end together. Acquaintance means an object only knows another, with separate lifetimes.

Example: a Booking owns its TimeSlot (aggregation). A Booking knows its Room, which exists before and after it (acquaintance).

Check yourself: 1. Which link does GoF call the weaker one? 2. Why are the two hard to tell apart in code? 3. Which is it when a Room outlives its bookings?