Example explanation of interface isolation principle in Java
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:
(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:
(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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
