Dependency Injection injects a class's dependencies from the outside, rather than instantiating them within the class. Because of this, classes are encouraged to have loose coupling, which facilitates changing dependencies or replacing them with alternative implementations.
Let's look at three classes of musicians: singers, drummers, and guitarists. In a band, every musician plays a certain part and needs the other musicians to play well together.
First, we are going to create the Musician interface:
interface IMusician {
play(): void;
}
Next, let's implement the classes for Guitarist, Drummer, and Singer:
class Guitarist implements IMusician {
play(): void {
console.log('Playing guitar');
}
}
class Drummer implements IMusician {
play(): void {
console.log('Playing drums');
}
}
class Singer implements IMusician {
play(): void {
console.log('Singing');
}
}
We will now create a class called Band which will require instances of Singer, Guitarist and Drummer as dependencies of the constructor method:
class Band {
constructor(singer: IMusician, guitarist: IMusician, drummer: IMusician) { }
performMusic(): void {
this.singer.play();
this.guitarist.play();
this.drummer.play();
}
}
Lastly, we will make instances of the Band, Singer, Guitarist and Drummer, and inject it as dependencies:
const band = new Band(
new Singer(),
new Guitarist(),
new Drummer()
);
band.performMusic();
In this example, the Band class implements the Dependency Injection principle by injecting Singer, Guitarist and Drummer instances as dependencies. This makes it simple to add new kinds of musicians (classic guitarist, jazz drummer or pop singer) or replace existing ones without changing the Band class.
You can get the source files from my GitHub repository.
I hope that this series was useful for you, thanks for your support.