当前位置:网站首页>Interface isolation principle

Interface isolation principle

2022-06-11 19:45:00 Wenhao_ it

All the following are summarized by me , Most of them come from learning .

Definition of interface isolation principle

         Interface isolation principle (Interface Segregation Principle,ISP) Programmers are required to try their best to split the bloated interface into smaller and more specific interfaces , Let the interface contain only methods of interest to the customer .  

        2002 Robert ·C. Martin to “ Interface isolation principle ” Is defined as : The client should not be forced to rely on methods it does not use (Clients should not be forced to depend on methods they do not use). There is another definition of this principle : The dependency of one class on another should be based on the smallest interface (The dependency of one class to another one should depend on the smallest possible interface).  

         The meaning of the above two definitions is : To create the specialized interfaces they need for each class , Instead of trying to build a very large interface for all classes that depend on it to call .  

         Interface isolation principle and Single responsibility All are To improve class cohesion 、 Reduce the coupling between them , It embodies the idea of encapsulation , But the two are different :

  • Principle of single responsibility Focus on responsibility , and Interface isolation principle Focus on the isolation of interface dependencies .
  • Principle of single responsibility Mainly constraint classes , It aims at the implementation and details in the program ; Interface isolation principle Main constraint interface , Mainly for the construction of abstraction and program framework .

The advantages of the interface isolation principle

The principle of interface isolation is to constrain interfaces 、 Reduce the class's dependency on the interface , Follow the interface isolation principle as follows 5 advantage .

  1. Decompose the bulky interface into several interfaces with small granularity , Can prevent the spread of external changes , Improve the flexibility and maintainability of the system .
  2. Interface isolation improves the cohesion of the system , Reduced external interaction , It reduces the coupling of the system .
  3. If the granularity of an interface is well defined , It can guarantee the stability of the system ; however , If the definition is too small , It will cause too many interfaces , Complicate the design ; If the definition is too big , Less flexibility , Can't provide custom services , Bring unexpected risks to the whole project .
  4. Using multiple specialized interfaces can also reflect the level of objects , Because it can be inherited through the interface , Implement the definition of the total interface .
  5. It can reduce code redundancy in project engineering . Many different methods are usually placed in large interfaces , When implementing this interface , Forced to design redundant code .

How to implement the principle of interface isolation

In the specific application of interface isolation principle , It should be measured according to the following rules .

  • The interface should be as small as possible , But to a limited extent . An interface serves only a sub module or business logic .
  • Customize services for interface dependent classes . Provide only the methods the caller needs , Shield unwanted methods .
  • Understand the environment , Refuse to follow blindly . Each project or product has selected environmental factors , The environment is different , The standard of interface splitting is different. We need to have a deep understanding of business logic .
  • Improve cohesion , Reduce external interaction . Make the interface do the most things in the least way .

 

Learn from the code of other websites :

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();
}

// Statistics module interface 
interface CountModule {
    void countTotalScore();

    void countAverage();
}

// Print module interface 
interface PrintModule {
    void printStuInfo();

    void queryStuInfo();
}

// Implementation class 
class StuScoreList : 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() {
        Console.WriteLine(" Input module insert() Method is called !");
    }

    public void delete() {
        Console.WriteLine(" Input module delete() Method is called !");
    }

    public void modify() {
        Console.WriteLine(" Input module modify() Method is called !");
    }

    public void countTotalScore() {
        Console.WriteLine(" Statistics module countTotalScore() Method is called !");
    }

    public void countAverage() {
        Console.WriteLine(" Statistics module countAverage() Method is called !");
    }

    public void printStuInfo() {
        Console.WriteLine(" Printing module printStuInfo() Method is called !");
    }

    public void queryStuInfo() {
        Console.WriteLine(" Printing module queryStuInfo() Method is called !");
    }
}

原网站

版权声明
本文为[Wenhao_ it]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111939381374.html