The Interface Segregation Principle (ISP) states that a client should not rely on interfaces it does not need, or that the dependencies of one class on another should be built on the smallest interface.
Programmers need to know!Practical Application and Case Study of the Interface Isolation Principle - Programmer Good.
The core idea of the interface isolation principle is the decoupling and reusability of the system. It encourages the breaking down of bloated interfaces into smaller, more specific interfaces, leaving the client (the class that uses the interface) to rely only on the interface methods it actually needs. The benefit of this is that the coupling between the client and the interface is reduced, making the system more flexible and easy to scale and maintain. Therefore, the principle of interface isolation encourages us to split interfaces into smaller, more specific interfaces, so that classes can rely on only the interfaces they really need. This reduces the coupling between classes and improves maintainability and scalability. At the same time, it also makes it clearer and easier to understand.
In actual development, we can implement the principle of interface isolation by splitting the interface into smaller interfaces, using adapter mode, and so on. At the same time, we also need to be careful not to over-abstract and over-design, so as not to increase the complexity and risk.
In J**A, the principle of interface isolation can be implemented in the following ways:
Interface splitting: Splitting a large and complete interface into multiple small, specialized interfaces, each containing only a set of related methods. Delegate pattern: By creating a new class to implement a subset of an interface, the client can rely on the new class instead of the interface as a whole. Multiple inheritance: In j**a, it is possible to make a class have the behavior of multiple interfaces by implementing multiple interfaces. This makes it possible to combine different interfaces as needed for more flexible functionality. The Interface Isolation Principle (ISP) is mainly applied in the following scenarios:
Large interface splittingWhen an interface is too large and contains many unrelated methods, consider splitting it into smaller, more specific interfaces. Client-side customization: When different clients need different parts of the same interface, you can split the interface so that each client depends only on the part of the interface it actually needs. Prevent fat interfaces: In the early stage of design, the granularity and specificity of the interface should be considered to avoid designing an overly large interface. Let's say we have a primitive massive interfaceallinoneprinter
, which includes a variety of functions such as printing, scanning, faxing, etc., as follows**:
@版权 Copyright by Programmer Goode
Created by Programmer Goode
Created: 2023 12 18 16:37
public interface allinoneprinter
public void dowork()
In this example,officeworker
Classes are actually only usedallinoneprinter
interfaceprint
method, but has to rely on the entire interface, including what it doesn't needscan
withfax
Method. This violates the principle of interface isolation. In order to follow the principle of interface isolation, we can set theallinoneprinter
The interface is split into smaller, more specific interfaces, as follows:
@版权 Copyright by Programmer Goode
Created by Programmer Goode
Created: 2023 12 18 16:37
public interface printer
public void dowork()
Now,officeworker
A class depends only on what it actually needsprinter
interface, which reduces unnecessary coupling and is more in line with the requirements of the interface isolation principle. If there are other classes that need to use the scan or fax function, they can depend on each otherscanner
orfaxmachine
interface, which enables a more flexible and maintainable structure.
The Interface Isolation Principle (ISP) is an object-oriented design principle that reduces the coupling between interfaces and improves reusability and maintainability by splitting large interfaces into multiple smaller, more specific interfaces. Benefits include reduced coupling, improved readability and maintainability, and enhanced scalability. However, excessive pursuit of the principle of interface isolation can lead to an excessive number of interfaces and an increase in the number of implementation classes. Therefore, the interface should be designed with specific business needs in mind and avoid excessive splitting;Be careful to maintain a moderate split when implementing;Maintain stability during use and avoid frequent modification of interface definitions.