Bird
Raised Fist0

Why is it important for the Product class to be immutable in a system where Cart and Order classes reference Product objects?

hard🧠 Conceptual Q10 of Q15
LLD - Design — Online Shopping Cart
Why is it important for the Product class to be immutable in a system where Cart and Order classes reference Product objects?
ATo reduce memory usage by sharing objects
BTo prevent accidental changes affecting all carts and orders
CTo enable faster database queries
DTo allow products to be deleted easily
Step-by-Step Solution
Solution:
  1. Step 1: Understand immutability benefits

    Immutable objects cannot be changed after creation, ensuring consistency.
  2. Step 2: Prevent side effects in Cart and Order

    If Product changes, all carts and orders referencing it would see inconsistent data.
  3. Final Answer:

    Immutability prevents accidental changes affecting all carts and orders -> Option B
  4. Quick Check:

    Immutability = data consistency [OK]
Quick Trick: Immutable products keep data consistent across system [OK]
Common Mistakes:
MISTAKES
  • Confusing immutability with deletion
  • Assuming immutability improves query speed
  • Thinking immutability reduces memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes