TOP8. Interface isolation principle-object-oriented design principle

         Definition of interface isolation principle

Advantages of the interface isolation principle

Implementation method of interface isolation principle


In addition to the open and closed principle , the Richter substitution principle , the dependency inversion principle, and the single responsibility principle , the object-oriented design principles also include the interface isolation principle, the Dimit's principle and the composite reuse principle. This section will introduce the interface isolation principle in detail.

Definition of interface isolation principle


The Interface Segregation Principle (ISP) requires programmers to split the bloated interface into smaller and more specific interfaces as much as possible, so that the interface contains only methods that are of interest to customers.

In 2002, Robert C. Martin defined the "interface isolation principle" as: Clients should not be forced to depend on methods they do not use. This principle has another definition: The dependency of one class to another one should depend on the smallest possible interface.

The meaning of the above two definitions is: to establish the dedicated interface they need for each class, instead of trying to establish a very large interface for all classes that rely on it to call.

The principle of interface isolation and single responsibility are both to improve the cohesion of classes and reduce the coupling between them, reflecting the idea of ​​encapsulation, but the two are different:

  • The single responsibility principle focuses on responsibility, while the interface isolation principle focuses on the isolation of interface dependencies.
  • The single responsibility principle is mainly the constraint class, which is aimed at the implementation and details in the program; the interface isolation principle mainly constrains the interface, and is mainly aimed at the construction of abstraction and the overall framework of the program.

Advantages of the interface isolation principle


The interface isolation principle is to restrict the interface and reduce the dependence of the class on the interface. Following the interface isolation principle has the following 5 advantages.

  1. Decomposing a bloated interface into multiple interfaces with small granularity can prevent the spread of external changes and improve the flexibility and maintainability of the system.
  2. Interface isolation improves the cohesion of the system, reduces external interaction, and reduces the coupling of the system.
  3. If the definition of the interface granularity is reasonable, the stability of the system can be ensured; 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. The overall project brings unforeseen risks.
  4. The use of multiple specialized interfaces can also reflect the level of the object, because the definition of the overall interface can be realized through the inheritance of the interface.
  5. Can reduce code redundancy in project engineering. There are usually many unused methods in a large interface that is too large. When implementing this interface, it is forced to design redundant codes.

Implementation method of interface isolation principle


In the specific application of 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 only serves one submodule or business logic.
  • Customize services for classes that rely on interfaces. Only provide the methods that the caller needs, and block the methods that are not needed.
  • Understand the environment and refuse to follow blindly. Each project or product has selected environmental factors. Different environments have different standards for interface splitting and in-depth understanding of business logic.
  • Improve cohesion and reduce external interaction. Make the interface use the least methods to accomplish the most things.


The following uses the student achievement management program as an example to introduce the application of the interface isolation principle.

[Example 1] Student performance management program.

Analysis: The student performance management program generally includes functions such as inserting scores, deleting scores, modifying scores, calculating total scores, calculating average scores, printing score information, querying score information, etc. It is obviously not reasonable to put all these functions in one interface , The correct way is to put them in three modules: input module, statistics module, and print module. The class diagram is shown in Figure 1.

Class diagram of student performance management program


                                                                           Figure 1 Class diagram of student performance management program

The program code is as follows:

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();
    }
}
// 输入模块接口
interface InputModule {
    void insert();
    void delete();
    void modify();
}
// 统计模块接口
interface CountModule {
    void countTotalScore();
    void countAverage();
}
// 打印模块接口
interface PrintModule {
    void printStuInfo();
    void queryStuInfo();
}
// 实现类
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("输入模块的insert()方法被调用!");
    }
    public void delete() {
        System.out.println("输入模块的delete()方法被调用!");
    }
    public void modify() {
        System.out.println("输入模块的modify()方法被调用!");
    }
    public void countTotalScore() {
        System.out.println("统计模块的countTotalScore()方法被调用!");
    }
    public void countAverage() {
        System.out.println("统计模块的countAverage()方法被调用!");
    }
    public void printStuInfo() {
        System.out.println("打印模块的printStuInfo()方法被调用!");
    }
    public void queryStuInfo() {
        System.out.println("打印模块的queryStuInfo()方法被调用!");
    }
}

The results of the program are as follows:

输入模块的insert()方法被调用!
统计模块的countTotalScore()方法被调用!
打印模块的printStuInfo()方法被调用!
<Single Responsibility Principle                                                                                              Dimit's Law>

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/106344466