The key to understanding the principle of interface separation is to understand the word “interface”.
In fact, the term “interface” can be used in many contexts. In software development, we can either see it as a set of abstract conventions, or it can refer specifically to the API interface between systems, or it can refer specifically to the interface in an object-oriented programming language.
Introduction to ISP
Understanding the Interface Segregation Principle
The Interface Segregation Principle states that no client should be forced to depend on methods it does not use. ISP emphasizes creating lean interfaces that are client-specific rather than general-purpose, bloated interfaces. This principle was introduced to combat the pitfalls of implementing large interfaces.
Why Embrace ISP?
Reduced Impact of Changes
- By segregating interfaces, changes in one part of the system have a minimal effect on clients using other parts of the interface.
Enhanced Code Clarity
- Smaller, well-defined interfaces are easier to understand and implement, leading to clearer and more maintainable code.
Improved Flexibility and Reusability
- Segregated interfaces allow for more flexible and reusable code. Different clients can use only the parts of the interface that are relevant to them.
What Is an “Interface”?
- In this principle, we can understand “interface” as the following three things:
- A set of API interfaces.
- A single API interface or function.
- The concept of interfaces in OOP.
A set of API interfaces
If you think of an “interface” as a collection of interfaces, it could be an interface to a microservice, a library, etc. If some of the interfaces are only used by some of the callers, we need to isolate those interfaces and give them to those callers alone, without forcing the other callers to rely on them.
A single API interface or function
If you think of an “interface” as a single API interface or function, and some callers only need some of the functionality in the function, then we need to split the function into more granular functions, so that the caller only relies on the fine-grained function that it needs.
The concept of interfaces in OOP
If you understand “interface” as an interface in OOP, you can also understand the interface syntax in object-oriented programming languages (e.g., interface
in Java). The design of the interface should be as simple as possible, so that the implementation class and the caller of the interface do not rely on unnecessary interface functions.
What are the differences between ISP and SRP?
Both ISP(Interface Segregation Principle) and SRP(Single Responsibility Principle) are designed to increase cohesion and reduce coupling. They both reflect the idea of encapsulation. But they have some differences:
- They emphasise the different levels:
- SRP emphasises singularity at the concrete level(e.g., business logic).
- ISP emphasises singularity at the abstract level(e.g., overall framework).
- They have different focuses:
- The focus of SRP is on responsibilities, and each class must remain singular in its own internal implementation.
- The focus of ISP is on interfaces, and each interface must remain singular in its external dependencies.