Conclusion
Composition and aggregation are two different ways to establish associative relationships in object-oriented programming. They form a “has-a” kind of relationship, which is relatively loose by definition compared with the “is-a” kind of relationship established with inheritance. However, composition enforces a stronger type of relationship where one object controls the other. The constituent object can’t live outside its host. But this isn’t so with aggregation, where each object is fully independent.
Here’s a summary of all you learned in this lesson:
- Composition and aggregation are two types of association in object-oriented programming.
- They both enforce loose coupling, are flexible, and increase code maintainability.
- Composite objects have ownership of the members they have. Their members live only as long as the composite objects themselves.
- Aggregation doesn’t enforce ownership. All related objects are independent.
In the next lesson, you’ll learn about some common design patterns used in object-oriented programming to make the code more maintainable, scalable, testable, and reusable.