DEV Community

Cover image for Interface Segregation Principle (ISP) in Typescript
Hasan Zohdy
Hasan Zohdy

Posted on

Interface Segregation Principle (ISP) in Typescript

Interface Segregation Principle In Typescript

Introduction

Interface Segregation Principle is the 4th principle of SOLID principles. It is also known as ISP principle. It was first introduced by Robert C. Martin in his 2000 paper Design Principles and Design Patterns.

Definition

The concept is simple, make the interfaces smaller and more specific to the use case. it is better to have multiple but smaller interfaces than a single large interface.

So basically, a class should not be forced to implement an interface or methods that it does not use.

Key Concepts Of ISP

  • Cohesion: Interfaces should be highly cohesive. It means the interface methods should be related to each other, if they are not related then they should be separated into different interfaces.
  • Responsibility: Interfaces should have single responsibility.

Example

Let's take an example of a Printer class that can print, scan and fax. So we can create an interface MultiFunctionPrinter that has all the methods of Printer class.

interface MultiFunctionPrinter {
  print(): void;
  scan(): void;
  fax(): void;
}
Enter fullscreen mode Exit fullscreen mode

Now we have a SimplePrinter class that can only print. So we can implement the MultiFunctionPrinter interface in SimplePrinter class.

class SimplePrinter implements MultiFunctionPrinter {
  print(): void {
    console.log("Printing...");
  }
  scan(): void {
    throw new Error("Simple Printer can not scan.");
  }
  fax(): void {
    throw new Error("Simple Printer can not fax.");
  }
}
Enter fullscreen mode Exit fullscreen mode

This is a total violation of ISP principle. Because SimplePrinter class is forced to implement the scan and fax methods that it does not use.

Solution

To make this run smoothly, we can separate the interface into three different interfaces.

interface Printer {
  print(): void;
}

interface Scanner {
  scan(): void;
}

interface Fax {
  fax(): void;
}
Enter fullscreen mode Exit fullscreen mode

Now we can implement the interfaces in SimplePrinter class.

class SimplePrinter implements Printer {
  print(): void {
    console.log("Printing...");
  }
}
Enter fullscreen mode Exit fullscreen mode

If there is advanced printer class, it can implement multiple interfaces.

class AdvancedPrinter implements Printer, Scanner, Fax {
  print(): void {
    console.log("Printing...");
  }
  scan(): void {
    console.log("Scanning...");
  }
  fax(): void {
    console.log("Faxing...");
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

ISP principle is very important to make the code more maintainable and flexible. It also helps to reduce the coupling between classes and not implementing unnecessary methods.

Following the list

You can see the updated list of design principles from the following link
https://mentoor.io/en/posts/634524154/open-closed-principle-in-typescript

Join us in our Discord Community
https://discord.gg/XDZcTuU8c8

Top comments (0)