Association
Classes that know about each other and can communicate.
Association is a relationship where one class knows about another class and can use its services. It’s a “uses-a” or “knows-a” relationship where classes can exist independently.
What is Association?
Section titled “What is Association?”Association represents a relationship where:
- Classes know about each other
- Classes can communicate with each other
- Classes have independent lifecycles
- Usually implemented with references or pointers
Key Characteristics
Section titled “Key Characteristics”- Independent existence - Both classes can exist without each other
- Communication - Classes can call each other’s methods
- References - One class holds a reference to another
- Weaker than composition - No ownership implied
Unidirectional Association
Section titled “Unidirectional Association”One class knows about another, but not vice versa.
Bidirectional Association
Section titled “Bidirectional Association”Both classes know about each other.
Visual Representation
Section titled “Visual Representation”Real-World Example: Library System
Section titled “Real-World Example: Library System”Key Takeaways
Section titled “Key Takeaways”When to Use Association
Section titled “When to Use Association”Use Association when:
- Classes need to communicate with each other
- Classes have independent lifecycles
- One class uses another’s services
- You need flexibility to change relationships
- Classes are loosely coupled
Examples:
- Teacher ↔ Course
- Student ↔ Course
- Order ↔ Product
- Loan ↔ Book
- Customer ↔ Order