CS FundamentalsHardGATE CS · TCS NQT Prime · Product Company SDE

A reporting class depends directly on a concrete EmailSender class to send alerts. A new requirement asks for alerts to optionally go via SMS instead. Applying SOLID principles, what is the recommended refactor?

  1. A. Add an if-statement inside EmailSender that checks whether SMS was requested
  2. B. Copy the entire reporting class and hardcode SMS logic into the duplicate
  3. C. Introduce an AlertSender abstraction that both EmailSender and a new SmsSender implement, and have the reporting class depend on that abstraction instead of the concrete EmailSender
  4. D. Make the reporting class extend both EmailSender and SmsSender simultaneously

Correct answer: C. Introduce an AlertSender abstraction that both EmailSender and a new SmsSender implement, and have the reporting class depend on that abstraction instead of the concrete EmailSender

Introducing a shared abstraction that concrete senders implement, and depending on that abstraction, follows both Dependency Inversion and Open/Closed: new sender types can be added without modifying the reporting class or existing senders.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic