Definition of interface isolation principles
What is an interface?
- Instance interface, such as defining a Person class, then Person p = new Pserson(); Create an instance, and the Person class is the interface to P
- A class interface is an interface defined in Java using interface
What is quarantine?
Isolation requires that the interface be as detailed as possible with as few methods in it as possible.
Implementation of the interface isolation principle
For example, there is a man who has multiple jobs. He is a teacher and a student and he has to learn. The picture is as follows:
If, one day, he is no longer teaching or has a new profession, then we need to modify the code that calls this class, or better yet, change the bloated interface to several separate interfaces
Called through the interface when in use. Interfaces are contracts that we provide externally at design time. By defining multiple interfaces in a decentralized manner, we can prevent the proliferation of future changes and improve the flexibility and maintainability of the system.
Specification constraints on interfaces
- The interface should be as small as possible, which is the core definition of the interface isolation principle, but there are limits to how small an interface should be, starting with the single responsibility principle.
- Interfaces should be highly cohesive, which means to improve the processing capacity of interfaces, classes, and modules and reduce external interactions. Specifically, it is required that public methods should be disclosed as little as possible in the interface. The interface is an external commitment, and the less commitment, the better for the development of the system, the less the risk of change, and the lower the cost
- Custom service, custom service is an individual to provide a good service, only provide visitors need methods
- Interface design is limited, the smaller the interface design granularity, the more flexible the system. However, flexibility also brings complexity of structure, increased difficulty of development and reduced maintainability, so the interface design must pay attention to moderation.
The interface isolation principle is the definition of both interfaces and classes, which are assembled using atomic interfaces or atomic classes as much as possible.