CS FundamentalsHardGATE CS · TCS NQT Prime · Product Company SDE

A high-level OrderService class directly instantiates and depends on a concrete MySqlDatabase class. What does the Dependency Inversion Principle recommend instead?

  1. A. Both OrderService and MySqlDatabase should depend on a shared abstraction (e.g. a Database interface), with OrderService depending on that interface rather than the concrete class directly
  2. B. High-level modules should never be allowed to call any low-level module at all, under any abstraction
  3. C. MySqlDatabase should directly extend OrderService
  4. D. OrderService should be merged into the same class as MySqlDatabase

Correct answer: A. Both OrderService and MySqlDatabase should depend on a shared abstraction (e.g. a Database interface), with OrderService depending on that interface rather than the concrete class directly

DIP says high-level modules should not depend on low-level modules directly; both should depend on abstractions, which is what lets OrderService later swap in a different database implementation without being rewritten.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic