This article is a summary of the study of “the Beauty of design Mode”
Interface Segregation Principle (ISP)
The English translation of the Interface isolation Principle is Interface Segregation Principle, abbreviated as ISP. Robert Martin defines it in the SOLID principle as follows: “Clients should not be forced to depend upon interface that they do not use.” A client should not be forced to rely on interfaces it does not need. The “client” can be understood as the caller or consumer of the interface.
We can think of “interfaces” as three things:
- A collection of API interfaces
- Single API interface or function
- The concept of interfaces in OOP
Think of “interface” as a set of API interfaces
We understand the interface in the interface isolation principle as a set of interfaces, which can be the interface of a microservice, can be the interface of a class library, etc. When designing microservice or class library interfaces, if part of the interface is only used by part of the callers, we need to isolate this part of the interface and make it available to the corresponding callers, rather than forcing other callers to rely on the interface that will not be used.
Think of “interface” as a single API interface or function
Another way to understand the interface is to understand the interface as a single interface or function, then the interface isolation principle can be understood as: the design of a single interface or function should be a single function, do not implement multiple different functional logic in a function.
Think of “interface” as an OOP concept of an interface
It can also be understood as the interface syntax in object-oriented programming language, the interface design should be as simple as possible, do not let the interface implementation class and the caller, do not rely on the interface function that is not needed.
The difference between the interface isolation principle and the single responsibility principle
The interface isolation principle is similar to the single responsibility principle, with some differences. The single responsibility principle addresses the design of modules, classes, and interfaces. Compared with the single responsibility principle, the interface isolation principle focuses more on the design of the interface on the one hand, and on the other hand, its thinking Angle is different. The interface isolation principle provides a way to determine whether an interface knows a single responsibility: indirectly by how the caller uses the interface. If the caller uses only part of the interface or part of the functionality of the interface, the interface design is not single-duty enough.