LLD Hub
Problems·OCP: Extend Discounts Without Modifying
SOLIDL2 · Easy

OCP: Extend Discounts Without Modifying

Problem

A PriceCalculator class uses a long if-else chain to apply discounts: if user is Student, apply 10%; if Festive season, apply 20%; if Premium member, apply 15%. Every new discount type requires modifying the calculator. Refactor to follow the Open/Closed Principle — open for extension, closed for modification.

Requirements

  • DiscountStrategy interface with: apply(originalPrice): number
  • StudentDiscount, FestiveDiscount, PremiumDiscount each implement it
  • PriceCalculator accepts a list of DiscountStrategy and applies them in order
  • New discount types (e.g. FlashSaleDiscount) added WITHOUT touching PriceCalculator
  • Discounts are composable — student + festive stacks

Constraints

  • PriceCalculator must have ZERO if-else or switch on discount type
  • Total discount should not exceed 50% of original price
✓ Saved