# Dependency Inversion Principle (DIP)

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710640300550/2797c593-f87a-4072-a225-7dafbfa4b7ba.png align="center")

First, we are going to create the Musician interface:

```typescript
interface IMusician {
  play(): void;
}
```

Next, let's implement the classes for Guitarist, Drummer, and Singer:

```typescript
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:

```typescript
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:

```typescript
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](https://github.com/maxmartinezc/solid-principles) repository.

I hope that this series was useful for you, thanks for your support.
