Composition vs Aggregation
Choose the correct has-a relationship by reasoning about ownership and whether component objects can exist independently.
Ownership changes the class relationship
- explain composition and aggregation;
- distinguish them using ownership and object lifecycle;
- design larger objects from smaller component objects;
- avoid confusing has-a relationships with inheritance.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
The containing object owns its parts
class LineItem:
def __init__(self, name, price):
self.name = name
self.price = price
class Order:
def __init__(self):
self.items = []
def add_item(self, name, price):
self.items.append(LineItem(name, price))Explain the relationship
The Order creates its own LineItem objects inside add_item(). Explain why this design can be described as composition.
The line items are created as internal parts of the order and are tightly owned by it. Their meaning/lifecycle is tied to that order rather than being independent shared objects.
The larger object refers to independent parts
class Member:
def __init__(self, name):
self.name = name
class Club:
def __init__(self, members):
self.members = members
m1 = Member("Kai")
m2 = Member("Zara")
club = Club([m1, m2])Explain the relationship
The Member objects are created before the Club and passed into it. Explain why this is aggregation.
The club refers to member objects that already exist independently. Removing the club would not require the member objects themselves to stop existing.
Use object independence to justify the relationship
Classify the relationships
For each pair, choose inheritance, composition or aggregation and justify: School–Teacher, House–Room, Manager–Employee.
- School–Teacher: aggregation is reasonable because teachers exist independently of one school object.
- House–Room: composition is a reasonable model because rooms are treated as integral parts of that house.
- Manager–Employee: inheritance if a manager is modeled as a specialized employee.
Lifecycle test
A Playlist stores references to Song objects that exist in a music library and can appear in several playlists. Composition or aggregation?
Aggregation. The songs exist independently, can be shared by multiple playlists and do not depend on one playlist for their existence.
Teacher-created Paper 2 practice
Paper 2 mini-set
1. Explain the difference between aggregation and composition.
2. A Project receives existing Employee objects from elsewhere and stores references to them. Explain the relationship.
3. A Game constructor creates its own internal ScoreBoard object, which is not shared elsewhere. Explain the relationship.
- Both are has-a relationships; aggregated components can exist independently, whereas composed components are tightly owned/lifecycle-dependent.
- Aggregation because the employees already exist independently and are only referenced by the project.
- Composition because the game creates and owns the scoreboard as an internal dependent component.
Can you separate is-a from has-a?
Exit check
Why is “Car inherits Engine” usually a bad model even though a car uses an engine?
A car is not a type of engine. The relationship is has-a, so composition/aggregation is more appropriate than inheritance.
Finish the learning cycle
IA — main task
Continue Criterion D development/testing. If your solution has multiple classes, identify one relationship and verify that its ownership/lifecycle model is intentional.
Paper 2 — short practice
Classify three real examples of your own as inheritance, aggregation or composition and justify each in one sentence.