Bird
Raised Fist0

Which design approach best models the relationship between Books, Copies, and Loans in a Library Management System to handle multiple copies of the same book being loaned independently?

easy🔍 Pattern Recognition Q2 of Q15
OOP & Design Patterns - Design a Library Management System - LLD with Relationships & Edge Cases
Which design approach best models the relationship between Books, Copies, and Loans in a Library Management System to handle multiple copies of the same book being loaned independently?
AUse inheritance where Copy inherits from Book and Loan inherits from Copy
BUse composition where Book contains multiple Copy objects, and each Copy can have zero or one Loan
CUse aggregation where Loan aggregates Book and Copy directly without intermediate objects
DUse a flat structure where Book, Copy, and Loan are unrelated classes with independent lifecycles
Step-by-Step Solution
Solution:
  1. Step 1: Understand the domain relationships between Book, Copy, and Loan

    A Book can have multiple physical Copies; each Copy can be loaned independently.
  2. Step 2: Evaluate inheritance versus composition for modeling relationships

    Inheritance is inappropriate because a Copy is not a specialized Book but a part of it; composition models "has-a" relationships correctly.
  3. Step 3: Analyze aggregation and flat structure drawbacks

    Aggregation without Copy as an intermediate loses clarity; flat structure ignores relationships and lifecycle dependencies.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Composition models multiple copies and loans accurately [OK]
Quick Trick: Copies are parts of Books -> composition fits best [OK]
Common Mistakes:
MISTAKES
  • Using inheritance for Copy from Book
  • Ignoring Copy as a separate entity
  • Flattening relationships losing lifecycle control
Trap Explanation:
PITFALL
  • Candidates often misuse inheritance for 'part-of' relationships, leading to poor design and confusion.
Interviewer Note:
CONTEXT
  • Reveals understanding of class relationships and composition vs inheritance.
Master "Design a Library Management System - LLD with Relationships & Edge Cases" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes