12 interface isolation principle - object oriented design principle

Posted by sparks on Tue, 02 Nov 2021 04:35:30 +0100

Opening and closing,Richter substitution,Dependency inversion,Single responsibility,Interface isolation,Dimitt,Synthetic multiplexing

In addition to the principles of opening and closing, Richter substitution, dependency inversion and single responsibility, the principles of object-oriented design also include the principles of interface isolation, Demeter's law and synthesis reuse. 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 bulky interface into smaller and more specific interfaces as far as possible, so that the interface only contains methods 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. The principle also 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 a special interface for each class, rather than trying to establish 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.

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.

  1. 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.
  2. Interface isolation improves the cohesion of the system, reduces external interaction and reduces the coupling of the system.
  3. 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.
  4. Using multiple special interfaces can also reflect the hierarchy of objects, because the definition of the general interface can be realized through interface inheritance.
  5. 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.

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.


The following introduces the application of the interface isolation principle by taking the student achievement management program as an example.

[example 1] student achievement management procedure.

Analysis: the student achievement management program generally includes the functions of inserting achievement, deleting achievement, modifying achievement, calculating total score, calculating average score, printing achievement information, querying achievement information, etc. if it is obviously unreasonable to put all these functions in one interface, the correct way is to put them in three modules: input module, statistics module and printing module, The class diagram is shown in Figure 1.


Figure 1 class diagram of student achievement 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();
    }
}

//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!");
    }
}

The running results of the program are as follows:

The insert() method of the input module is called!
The countTotalScore() method of the statistics module is called!
printStuInfo() method of print module is called!

Return to home directory: Comprehensive analysis of 23 design modes (super detailed)

In order to reprint the article, if there is infringement, please contact me for deletion.

This article is reproduced from: Comprehensive analysis of 23 design modes (super detailed)

Topics: Design Pattern