Home >Backend Development >PHP Tutorial >What is the interface isolation principle?
The client should not rely on interfaces it does not need; the dependence of one class on another class should be based on the smallest interface.
It is better to use multiple specialized interfaces than a single general interface.
The dependence of one class on another class should be based on the smallest interface.
An interface represents a role, and different roles should not be assigned to one interface. Interfaces that are not related are merged together to form a large and bloated interface, which is a pollution to roles and interfaces.
"Clients should not be forced to rely on methods they do not use. The interface belongs to the client, not the class hierarchy in which it is located." This is very clear. To put it more generally, do not force clients to use methods they do not use. methods, if users are forced to use methods they do not use, then these customers will face changes caused by changes in these methods they do not use.
What this picture means is: Class A depends on method 1, method 2, and method 3 in interface I, and class B is the implementation of class A's dependence. Class C depends on method 1, method 4, and method 5 in interface I. Class D is the implementation of dependence on class C. For classes B and D, although they both have unused methods (that is, the methods marked in red font in the figure), since interface I is implemented, these unused methods must also be implemented. It can be seen that if the interface is too bloated, as long as the methods appear in the interface, regardless of whether they are useful to the classes that depend on it, these methods must be implemented in the implementation class. This is obviously not a good design. If this design is modified to comply with the interface isolation principle, interface I must be split. Here we split the original interface I into three interfaces. The split design is as shown in the figure
The meaning of the interface isolation principle is: establish a single interface , do not build a huge and bloated interface, try to refine the interface, and try to have as few methods in the interface as possible. In other words, we need to establish dedicated interfaces for each class, rather than trying to build a huge interface for all classes that rely on it to call. In the example in this article, the interface isolation principle is used to change a huge interface into three dedicated interfaces. In programming, it is more flexible to rely on several dedicated interfaces than to rely on one comprehensive interface. Interfaces are "contracts" set externally during design. By decentrally defining multiple interfaces, we can prevent the spread of external changes and improve the flexibility and maintainability of the system.
Speaking of this, many people will think that the interface isolation principle is very similar to the previous single responsibility principle, but it is not. First, the single responsibility principle originally focused on responsibilities; while the interface isolation principle focused on the isolation of interface dependencies. Secondly, the single responsibility principle mainly constrains classes, followed by interfaces and methods, and it targets the implementation and details of the program; while the interface isolation principle mainly constrains interfaces, mainly for abstraction, and for the construction of the overall framework of the program.
When using the interface isolation principle to constrain interfaces, pay attention to the following points:
The interface should be as small as possible, but within limits. It is a fact that refining the interface can improve programming flexibility, but if it is too small, it will cause too many interfaces and complicate the design. So it must be done in moderation.
Customize services for classes that rely on interfaces, exposing only the methods it needs to the calling class, and hiding the methods it doesn't need. Only by focusing on providing customized services for a module can minimal dependencies be established.
Improve cohesion and reduce external interaction. Make the interface use the fewest methods to accomplish the most things.
The principle of interface isolation must be used in moderation. It is not good to design the interface too large or too small. When designing interfaces, only by spending more time thinking and planning can you accurately implement this principle.
The above is the detailed content of What is the interface isolation principle?. For more information, please follow other related articles on the PHP Chinese website!