# I - Interface Segregation (ISP)

## About

A class should not implement interfaces that it won't use.

{% hint style="info" %}
Create interfaces based only on what the client needs, avoiding dependencies on things it won't use.
{% endhint %}

{% hint style="success" %}
Usually impacts reduction of unecessary re-compilation.
{% endhint %}

<img src="https://918994881-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCohgIgtQvaflZD82x7YP%2Fuploads%2FwMEGoSNweqkEI3b4B9eR%2Ffile.excalidraw.svg?alt=media&#x26;token=fce4af66-be06-45ea-9c26-70075162aa1c" alt="" class="gitbook-drawing">

## Example

In this example, the `Exec` class, which is the class that uses the method, specifies what it wants from `ClassA`.

So now, `Exec` don't know `ClassA` anymore, it only knows `Interface2`. And if `ClassA` changes in the future, `Exec` won't need to be re-compiled *(Since there is no more* `import A from '.../ClassA'`*)*.

{% code title="Main.ts" %}

```typescript
Registry.getInstance().provide('classA', new A());
```

{% endcode %}

{% code title="Exec.ts" %}

```typescript
class Exec {
    @inject('classA')
    classA: Interface2;
}

export interface2 {
    method2(): void;
}
```

{% endcode %}

{% code title="ClassA.ts" %}

```typescript
interface ARepo extends Interface1, Interface2 {
    method1(): void;
    method2(): void;
    method3(): void;
}

class A implements ARepo {
    method1(): void { ... }
    method2(): void { ... }
    method3(): void { ... }
}
```

{% endcode %}
