1. Definition of interface isolation principle
Instead of trying to build a huge interface for all classes that depend on it to call.
The principle of interface isolation and single responsibility are to improve the cohesion of classes and reduce the coupling between them, reflecting the idea of encapsulation, but they are different:
- The single responsibility principle focuses on responsibilities, while the interface isolation principle focuses on the isolation of interface dependencies.
- The single responsibility principle is mainly a constraint class, which aims at the implementation and details of the program; The principle of interface isolation mainly restricts the interface, and mainly aims at the construction of abstraction and the overall framework of the program.
2. Advantages of interface isolation principle
The interface isolation principle is to restrict the interface and reduce the dependence of classes on the interface. Following the interface isolation principle has the following five advantages.
- Decomposing a bulky interface into multiple interfaces with small granularity can prevent the proliferation of external changes and improve the flexibility and maintainability of the system.
- Interface isolation improves the cohesion of the system, reduces external interaction and reduces the coupling of the system.
- If the granularity of the interface is reasonably defined, the stability of the system can be guaranteed; However, if the definition is too small, it will cause too many interfaces and complicate the design; If the definition is too large, the flexibility will be reduced, and customized services cannot be provided, which will bring unpredictable risks to the overall project.
- Using multiple special interfaces can also reflect the hierarchy of objects, because the definition of the general interface can be realized through interface inheritance.
- It can reduce code redundancy in project engineering. Many unused methods are usually placed in too large interfaces. When implementing this interface, redundant code is forced to be designed.
3. Implementation method of interface isolation principle
When applying the interface isolation principle, it should be measured according to the following rules.
- The interface should be as small as possible, but limited. An interface serves only one sub module or business logic.
- Customize services for classes that depend on interfaces. Only the methods required by the caller are provided, and the methods not required are shielded.
- Understand the environment and refuse to follow blindly. Each project or product has selected environmental factors. Different environments lead to different standards for interface splitting, and in-depth understanding of business logic.
- Improve cohesion and reduce external interaction. Make the interface do the most with the least methods.
4. Student achievement management procedure
package principle; public class ISPtest { public static void main(String[] args) { InputModule input = StuScoreList.getInputModule(); CountModule count = StuScoreList.getCountModule(); PrintModule print = StuScoreList.getPrintModule(); input.insert(); count.countTotalScore(); print.printStuInfo(); //print.delete(); } } //Input module interface interface InputModule { void insert(); void delete(); void modify(); } //Statistical module interface interface CountModule { void countTotalScore(); void countAverage(); } //Print module interface interface PrintModule { void printStuInfo(); void queryStuInfo(); } //Implementation class class StuScoreList implements InputModule, CountModule, PrintModule { private StuScoreList() { } public static InputModule getInputModule() { return (InputModule) new StuScoreList(); } public static CountModule getCountModule() { return (CountModule) new StuScoreList(); } public static PrintModule getPrintModule() { return (PrintModule) new StuScoreList(); } public void insert() { System.out.println("Input module insert()Method called!"); } public void delete() { System.out.println("Input module delete()Method called!"); } public void modify() { System.out.println("Input module modify()Method called!"); } public void countTotalScore() { System.out.println("Of statistical module countTotalScore()Method called!"); } public void countAverage() { System.out.println("Of statistical module countAverage()Method called!"); } public void printStuInfo() { System.out.println("Printing module printStuInfo()Method called!"); } public void queryStuInfo() { System.out.println("Printing module queryStuInfo()Method called!"); } }