What is the difference between association, aggregation and composition?
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
When designing software, you often need to define relationships between objects or classes. These relationships—association, aggregation, and composition—help model real-world connections in your code. Let's explore these concepts with a simple example before diving into the details.
Real-World Example
Imagine a school:
- Association: Teachers and students are part of the school. A teacher can teach multiple students, and a student can learn from multiple teachers. They are connected but independent—neither exists because of the other.
- Aggregation: The school has classrooms. Classrooms belong to the school, but they can exist independently. Even if the school closes, the classrooms may be used elsewhere.
- Composition: The school has desks in the classrooms. These desks cannot exist without the classrooms. If a classroom is removed, its desks are also destroyed.
Key Differences Between Association, Aggregation, and Composition
Association
- Definition: A general relationship where two objects are connected but independent.
- Characteristics:
- Represents a "uses" or "works with" relationship.
- Neither object owns the other.
- Example: A teacher teaches students, but both can exist independently.
- UML Representation: A plain line between objects.
Aggregation
- Definition: A "has-a" relationship where one object contains another, but the contained object can exist independently.
- Characteristics:
- Represents a "whole-part" relationship.
- The part can exist even if the whole is removed.
- Example: A school has classrooms, but classrooms can exist without the school.
- UML Representation: A hollow diamond at the "whole" end.
Composition
- Definition: A stronger "has-a" relationship where one object contains another, and the contained object cannot exist independently.
- Characteristics:
- Represents a "whole-part" relationship with ownership.
- The part is destroyed when the whole is destroyed.
- Example: A classroom has desks that are destroyed if the classroom is removed.
- UML Representation: A filled diamond at the "whole" end.
Practical Use in Code
Association
class Teacher: pass class Student: pass # Association: A teacher can teach students, but both exist independently. teacher = Teacher() student = Student()
Aggregation
class School: def __init__(self): self.classrooms = [] # Aggregates classrooms class Classroom: pass # Aggregation: Classrooms exist independently of the school. school = School() classroom = Classroom() school.classrooms.append(classroom)
Composition
class Classroom: def __init__(self): self.desks = [] # Composes desks class Desk: pass # Composition: Desks cannot exist without the classroom. classroom = Classroom() desk = Desk() classroom.desks.append(desk)
Summary
- Association: Loose connection, objects are independent.
- Aggregation: One object owns another but doesn't control its lifecycle.
- Composition: Strong ownership, the lifecycle of the part depends on the whole.
To strengthen your system design and object-oriented programming skills, explore DesignGurus.io's Grokking System Design Fundamentals or Grokking the System Design Interview. These courses explain such concepts in practical scenarios.
TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.