CS FundamentalsVery hardProduct-company design interview

A class Report depends on ReportFormatter, and ReportFormatter depends on Report to read its data, creating a cyclic dependency between their packages. Which design change breaks the cycle in line with the Dependency Inversion Principle?

  1. A. Merge both packages into one
  2. B. Make all fields public
  3. C. Have ReportFormatter depend on an abstraction such as a ReportData interface owned by its own package, which Report implements
  4. D. Duplicate the formatter code inside Report

Correct answer: C. Have ReportFormatter depend on an abstraction such as a ReportData interface owned by its own package, which Report implements

Introducing an interface and inverting one dependency turns the cycle into a one-way graph (the Acyclic Dependencies Principle). Merging hides the problem; duplication creates another.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic