Dependency Inversion Principle

High-level classes should not depend on low-level classes; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. This inverts the typical direction of dependency so that business logic is insulated from changes in infrastructure.

Examples:

  • A BudgetReport class that directly uses a MySQLDatabase class breaks whenever the database changes. Introduce a ReportingStorage interface declared in the business-logic layer. BudgetReport depends on the interface; MySQLDatabase implements it. A later switch to PostgreSQL only requires a new implementation — BudgetReport is untouched.

Synonyms: dip