All Together
See all relationships working together in a real-world system.
Let’s build a complete Library Management System that demonstrates all four class relationships working together.
System Overview
Section titled “System Overview”We’ll design a library system with:
- Library (main system)
- Book (items in library)
- Member (people who borrow books)
- Librarian (staff managing library)
- Loan (borrowing record)
- Fine (penalty for late returns)
Complete Implementation
Section titled “Complete Implementation”Relationship Summary
Section titled “Relationship Summary”Relationship Breakdown
Section titled “Relationship Breakdown”1. Inheritance (Person → Member, Librarian)
Section titled “1. Inheritance (Person → Member, Librarian)”- Why: Both share common attributes (name, email) and have roles
- Type: “Is-a” relationship
2. Composition (Library → Book, Member → Loan, Loan → Fine)
Section titled “2. Composition (Library → Book, Member → Loan, Loan → Fine)”- Why: Books are part of library, loans belong to members, fines belong to loans
- Type: Strong ownership, cannot exist independently
3. Aggregation (Library → Member, Library → Librarian)
Section titled “3. Aggregation (Library → Member, Library → Librarian)”- Why: Members and librarians can exist independently (can transfer/leave)
- Type: Weak ownership, independent lifecycle
4. Association (Loan → Member, Book, Librarian)
Section titled “4. Association (Loan → Member, Book, Librarian)”- Why: Loan references these classes but doesn’t own them
- Type: “Knows-a” relationship
5. Dependency (Member → Book, Librarian → Loan)
Section titled “5. Dependency (Member → Book, Librarian → Loan)”- Why: Used temporarily as method parameters
- Type: Temporary use, no storage