search
HomeJavajavaTutorialExample explanation of interface isolation principle in Java

Example explanation of interface isolation principle in Java

Aug 09, 2017 pm 04:15 PM
javain principleExample

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software