Design Principles - Interface Isolation Principle

Interface isolation principle: extract common features from the same abstract class or interface, and extract a separate interface for extension for features. Each class establishes its own dedicated interface instead of establishing a universal interface.

meaning

  • The interface isolation principle ( Interface Segregation Principle, ISP) refers to the use of multiple specialized interfaces instead of a single overall interface. The client should not rely on interfaces it does not need. The principle of interface isolation conforms to the design idea of ​​high cohesion and low coupling that we often say, so that the class has good readability, scalability, and maintainability.

main idea

  • High cohesion and low coupling
  • A class should not rely on interfaces it does not need. That is, the dependence between classes should be based on the minimum interface.

advantage

  • Interface isolation improves the cohesion of the system, reduces external interactions, and reduces the coupling of the system.
  • Decomposing bloated interfaces into multiple small-granularity interfaces can prevent the spread of external changes and improve the flexibility, scalability and maintainability of the system.

The difference between interface isolation and the single responsibility principle

  • The single responsibility principle originally focused on responsibilities; while the interface isolation principle focused on the isolation of interface dependencies.
  • The single responsibility principle mainly constrains classes, followed by interfaces and methods, and is aimed at the implementation and details of the program; while the interface isolation principle mainly constrains interfaces, mainly for abstraction, and for the construction of the overall framework of the program.

Method to realize

  • The dependence of a class on a class should be based on the smallest interface
  • Establish a single interface to improve cohesion and reduce external interactions. Do not build huge and bloated interfaces. Make the interface use the least methods to accomplish the most things.
  • Try to refine the interface as much as possible. There should be as few methods in the interface as possible, but there must be a limit. The fewer the better, it must be moderate. If the interface is too small, there will be too many interfaces and the design will be complicated. Therefore, classes that rely on the interface must be A custom service exposes only the methods it needs to the calling class, and hides the methods it doesn't need. Only by focusing on providing customized services for a module can minimal dependencies be established.
  • Spend more time thinking and considering the business model, including making some predictions about possible changes in the future. Therefore, it is very important to understand abstraction and business models.

Case

Take animal behavior as an example, create a new animal interface, add behaviors such as eating, sleeping, flying, swimming, and crawling, and then add a fish to implement the animal interface.

Insert image description here

Here’s the problem, fish can’t fly or crawl, so there’s no way to implement these two interfaces, which means that fish are forced to rely on methods it doesn’t use, that is, flying and crawling methods. , which also violates the principle of interface isolation

Case improvements

Abstract the common characteristics of animals (eating, sleeping), and then abstract the interfaces that fish do not need separately. That is, the common characteristics are extracted into the same abstract class or interface, and the characteristics are extracted into a separate interface for expansion. Which interfaces do you need in the future and which interfaces do you implement? This reduces the scope of the interface to the minimum.

Insert image description here

public interface Animal {
    
    
    void eat();
    void sleep();
}
public interface Birds{
    
    
    void fly();
}
public interface Fish {
    
    
    void swim();
}
public class Eagle implements Animal,Birds{
    
    

    @Override
    public void eat() {
    
    
        System.out.println("老鹰在吃东西");
    }

    @Override
    public void sleep() {
    
    
        System.out.println("老鹰在睡觉");
    }

    @Override
    public void fly() {
    
    
        System.out.println("老鹰在飞翔");
    }
}
public class Salmon implements Animal,Fish{
    
    

    @Override
    public void eat() {
    
    
        System.out.println("三文鱼在吃东西");
    }

    @Override
    public void sleep() {
    
    
        System.out.println("三文鱼在睡觉");
    }

    @Override
    public void swim() {
    
    
        System.out.println("三文鱼在游动");
    }
}

Guess you like

Origin blog.csdn.net/qq_42700109/article/details/132838516