Dependency Injection is a software design pattern that reduces coupling in code. Low-coupled code is more flexible, making it easier to change and test.
A simple example is that business logic depends on a set of data. This data could be stored in files, memory, or a database. The applicationโs business logic shouldnโt care how or where the data is stored, only that it can be stored and read back.
This can be visualised with the following class diagram
classDiagram
BusinessLogic--*DataStore
Database--|>DataStore
Filestore--|>DataStore
Application..>BusinessLogic : Injects DataStore
Application..>Database : Creates
Database and Filestore both implement the interface of DataStore. The Application then creates an instance of either Database or Filestore and injects it into BusinessLogic.