Home  >  Article  >  Java  >  Example explanation of interface isolation principle in Java

Example explanation of interface isolation principle in Java

黄舟
黄舟Original
2017-08-09 16:15:123125browse

This article mainly introduces the principle of interface isolation. The editor thinks it is quite good. I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

Definition: The client should not rely on interfaces it does not need; the dependence of one class on another class should be based on the smallest interface.

Origin of the problem: Class A depends on class B through interface I, class C depends on class D through interface I, if interface I is not the minimum interface for class A and class B , then class B and class D must implement methods they do not need.

Solution: Split the bloated interface I into several independent interfaces, and class A and class C establish dependencies with the interfaces they need. That is to say, the principle of interface isolation is adopted.
Take an example to illustrate the interface isolation principle:

Example explanation of interface isolation principle in Java

(Figure 1 Design that does not follow the interface isolation principle)

The meaning of this picture is : Class A depends on method 1, method 2, and method 3 in interface I. Class B is the implementation of class A's dependence. Class C depends on method 1, method 4, and method 5 in interface I. Class D is the implementation of dependence on class C. For classes B and D, although they both have unused methods (that is, the methods marked in red font in the figure), since interface I is implemented, these unused methods must also be implemented. Those who are not familiar with class diagrams can refer to the program code to understand it. The code is as follows:


interface I {
  public void method1();
  public void method2();
  public void method3();
  public void method4();
  public void method5();
}

class A{
  public void depend1(I i){
    i.method1();
  }
  public void depend2(I i){
    i.method2();
  }
  public void depend3(I i){
    i.method3();
  }
}

class B implements I{
  public void method1() {
    System.out.println("类B实现接口I的方法1");
  }
  public void method2() {
    System.out.println("类B实现接口I的方法2");
  }
  public void method3() {
    System.out.println("类B实现接口I的方法3");
  }
  //对于类B来说,method4和method5不是必需的,但是由于接口A中有这两个方法,
  //所以在实现过程中即使这两个方法的方法体为空,也要将这两个没有作用的方法进行实现。
  public void method4() {}
  public void method5() {}
}

class C{
  public void depend1(I i){
    i.method1();
  }
  public void depend2(I i){
    i.method4();
  }
  public void depend3(I i){
    i.method5();
  }
}

class D implements I{
  public void method1() {
    System.out.println("类D实现接口I的方法1");
  }
  //对于类D来说,method2和method3不是必需的,但是由于接口A中有这两个方法,
  //所以在实现过程中即使这两个方法的方法体为空,也要将这两个没有作用的方法进行实现。
  public void method2() {}
  public void method3() {}

  public void method4() {
    System.out.println("类D实现接口I的方法4");
  }
  public void method5() {
    System.out.println("类D实现接口I的方法5");
  }
}

public class Client{
  public static void main(String[] args){
    A a = new A();
    a.depend1(new B());
    a.depend2(new B());
    a.depend3(new B());

    C c = new C();
    c.depend1(new D());
    c.depend2(new D());
    c.depend3(new D());
  }
}

As you can see, if the interface is too bloated, as long as the methods that appear in the interface , regardless of whether it is useful for classes that depend on it, these methods must be implemented in the implementation class. This is obviously not a good design. If this design is modified to comply with the interface isolation principle, interface I must be split. Here we split the original interface I into three interfaces. The split design is shown in Figure 2:

Example explanation of interface isolation principle in Java

(Figure 2 follows the interface isolation principle Design)

Post the code of the program as usual for the reference of friends who are not familiar with class diagrams:


interface I1 {
  public void method1();
}

interface I2 {
  public void method2();
  public void method3();
}

interface I3 {
  public void method4();
  public void method5();
}

class A{
  public void depend1(I1 i){
    i.method1();
  }
  public void depend2(I2 i){
    i.method2();
  }
  public void depend3(I2 i){
    i.method3();
  }
}

class B implements I1, I2{
  public void method1() {
    System.out.println("类B实现接口I1的方法1");
  }
  public void method2() {
    System.out.println("类B实现接口I2的方法2");
  }
  public void method3() {
    System.out.println("类B实现接口I2的方法3");
  }
}

class C{
  public void depend1(I1 i){
    i.method1();
  }
  public void depend2(I3 i){
    i.method4();
  }
  public void depend3(I3 i){
    i.method5();
  }
}

class D implements I1, I3{
  public void method1() {
    System.out.println("类D实现接口I1的方法1");
  }
  public void method4() {
    System.out.println("类D实现接口I3的方法4");
  }
  public void method5() {
    System.out.println("类D实现接口I3的方法5");
  }
}

Interface isolation principle The meaning is: build a single interface, do not build a huge and bloated interface, try to refine the interface, and try to have as few methods in the interface as possible. In other words, we need to establish dedicated interfaces for each class, rather than trying to build a huge interface for all classes that rely on it to call. In the example in this article, the interface isolation principle is used to change a huge interface into three dedicated interfaces. In programming, it is more flexible to rely on several specialized interfaces than to rely on one comprehensive interface. Interfaces are "contracts" set externally during design. By decentrally defining multiple interfaces, we can prevent the spread of external changes and improve the flexibility and maintainability of the system.

Speaking of this, many people will think that the interface isolation principle is very similar to the previous single responsibility principle, but it is not. First, the single responsibility principle originally focused on responsibilities; while the interface isolation principle focused on the isolation of interface dependencies. Secondly, the single responsibility principle mainly constrains classes, followed by interfaces and methods, and it targets 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.

When using the interface isolation principle to constrain interfaces, pay attention to the following points:

  • The interface should be as small as possible, but within limits. It is a fact that refining the interface can improve programming flexibility, but if it is too small, it will cause too many interfaces and complicate the design. So it must be done in moderation.

  • Customize services for classes that rely on interfaces, exposing only the methods it needs to the calling class, and hiding the methods it doesn’t need. Only by focusing on providing customized services for a module can minimal dependencies be established.

  • Improve cohesion and reduce external interaction. Make the interface use the fewest methods to accomplish the most things.

The principle of interface isolation must be used in moderation. It is not good to design the interface too large or too small. When designing an interface, only by spending more time thinking and planning can you accurately practice this principle

The above is the detailed content of Example explanation of interface isolation principle in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn