AlgoViz

Dependency inversion (DIP)

medium 40 min

The rules of your program should not name the tools they run on.

Watch lesson4:45

Where it shows up

The same pattern appears across everyday software.

  • JDBC

    Java code calls interfaces in `java.sql`. Every database vendor ships a class that implements `Driver`. The same calls work over different drivers; the SQL text may still differ.

  • Go database/sql

    The package holds the generic calls and must be used with a driver. Package `database/sql/driver` defines the interfaces that drivers implement.

  • Kubernetes CSI

    Storage vendors write plugins against the Container Storage Interface without touching the core code of Kubernetes.

  • Spring

    The container passes beans to constructors. The Spring team generally advocates constructor injection, so objects can be immutable and needed parts are not null.

  • SLF4J

    Library code logs to a facade. The application chooses Logback or another provider when it is deployed.

  • React context

    A parent supplies a value and any component below reads it without knowing who supplied it. Tests wrap the component in another provider. It is injection; inversion only if the value is an interface you own.

  • VS Code extensions

    The editor states the `TreeDataProvider` interface. An extension implements and registers it, and the editor calls it to fill a view.

  • FastAPI

    A handler declares what it depends on. Tests put another function in `app.dependency_overrides`, so no external service is called.

  • Dagger and Hilt

    On Android, Hilt supplies containers for Android classes and is built on Dagger, which checks the graph at compile time.