Best Design Principles in Angular
Part 4: Understanding the Interface Segregation Principle (ISP)
Hey there! So, you want to know how to make the best code design for your Angular application? In this article, we take a closer look at the Interface Segregation Principle (ISP). Let’s dive into it and see how it can make our code better.
So, ISP is all about not forcing clients to depend on stuff they don’t need. Imagine you have a bunch of methods in an interface, but different clients only need a few of them. With ISP, we split that big interface into smaller ones, each serving a specific purpose. This way, clients can just depend on the interfaces they actually need, making things more flexible and avoiding unnecessary dependencies.
Now, let’s see some examples to make it clearer.
Also, check out these Angular dev tools for 2023.
Example 1: Without ISP in Angular
Imagine we have an EmployeeService interface with methods for adding, updating, deleting, and getting employees. We also have a class called EmployeeManager that implements this interface. The problem is that some clients might only need to get or delete employees, but they still have to depend on the entire EmployeeService interface. Not cool, right? This violates ISP because clients are forced to rely on methods they don't even use.
But fear not, ISP comes to the rescue! In the “with ISP” scenario, we split the EmployeeService interface into two smaller interfaces: EmployeeReader and EmployeeWriter. The EmployeeReader interface has methods for getting employee info, while the EmployeeWriter interface handles adding, updating, and deleting employees. Now clients can choose to depend only on the interface that suits their needs. Isn't that neat?
Alright, now let’s take a look at how ISP works with other SOLID principles, about which I have already written in previous articles:
- Single Responsibility Principle (SRP), says a class should have only one reason to change. It’s like having a class that focuses on doing just one thing. ISP supports SRP by encouraging smaller interfaces with specific responsibilities. So, each interface follows the SRP and changes to one interface won’t mess up the others.
- Open/Closed Principle (OCP) suggests that software entities should be open for extension but closed for modification. ISP goes hand in hand with OCP by allowing easy extension through new interfaces and implementations without modifying existing code. You can add new clients by just implementing the required interface. No need to change the existing code. Pretty convenient, right?
- Liskov Substitution Principle (LSP) tells us that objects of a superclass should be replaceable with objects of its subclasses without causing trouble. ISP complements LSP by making sure that interfaces are cohesive and focused. When interfaces are segregated properly, substituting one implementation with another won’t break the expected behavior. It’s all about maintaining consistency and avoiding unexpected surprises.
💡 As an aside, no matter which design principle you apply, using an open-source tool like Bit can streamline your development process. Bit’s component-driven development approach lets you build independent Angular components that can be tested and shared across multiple projects with a simple bit import your.username/yourComponent command.Learn more:
In summary, ISP is a cool principle that helps us keep our code clean, flexible, and adaptable. By splitting interfaces into smaller ones, we can avoid unnecessary dependencies and make our codebase more manageable. And when combined with SRP, OCP, and LSP, it’s like having a superhero team that makes our code even better!
Thanks for your attention, if you like this article you can 👏 , follow me and write a comment.
Build Angular Apps with reusable components, just like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want: