Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Java object-oriented Interface isolation principle

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to implement the Java object-oriented interface isolation principle". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the Java object-oriented interface isolation principle.

Define

Interface Segregation Principle

The client should not rely on interfaces it does not need

Dependencies between classes should be based on the smallest interface

In fact, the popular understanding is, do not put a lot of methods in an interface, this will make this class look very bloated. The interface should be as detailed as possible, one interface corresponds to a functional module, and the methods in the interface should be as few as possible to make the interface more flexible and portable. Some people may think that the principle of interface isolation is very similar to the principle of single responsibility, but there are still obvious differences between the two principles. The principle of single responsibility is the division of business logic, focusing on responsibilities. The principle of interface isolation is based on interface design considerations. For example, the responsibility of an interface contains 10 methods, all of which are placed in the same interface and are provided to multiple modules to call, but different modules need to rely on different methods. At this time, the module has to implement some methods that do not make sense to it in order to achieve its own functions. This design is not in line with the principle of interface isolation. The principle of interface isolation requires that "as many specialized interfaces as possible" are specifically provided to different modules.

Origin

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

For example:

Public interface School {

/ * *

* classes

, /

Void attendClass ()

/ * *

* dismissed from class

, /

Void afterClass ()

/ * *

* Learning

, /

Void learn ()

/ * *

* lectures

, /

Void lecture ()

}

Suppose there is a People class whose role is a student and implements the School interface. It will be forced to implement the "lecture" method, which is actually not needed. This creates redundancy in the code and makes our code bloated.

Solve

The bloated interface I is divided into several independent interfaces, and class An and class C respectively establish dependencies with the interfaces they need.

For example:

School interface

Public interface School {

/ * *

* classes

, /

Void attendClass ()

/ * *

* dismissed from class

, /

Void afterClass ()

}

Teacher interface

Public interface Teacher {

/ * *

* lectures

, /

Void lecture ()

}

Student interface

Public interface School {

/ * *

* Learning

, /

Void learn ()

}

Through the above split, we can effectively avoid the generation of redundant code, which in turn can make our code more flexible.

Advantages to avoid interface contamination

If a class wants to implement an interface, then it is necessary to implement all the methods required by the interface. If the interface contains methods that the class does not need, it will cause interface pollution, which is a bad design and will leave hidden dangers to the system.

Increase flexibility

A class can implement multiple interfaces at the same time, so a bloated interface can be divided into several small interfaces to meet more requirements through different combinations of small interfaces.

Provide customized services

Customized service is to provide excellent service for an individual. When we do the system design, we also need to consider providing customized services for the interfaces between systems or modules. To provide custom services, there must be a requirement: to provide only the methods that visitors need. This can also be achieved by refining the interface.

High cohesion

What is high cohesion? High cohesion is to improve the processing ability of interfaces, classes and modules, and reduce external interaction. For example, you tell your subordinates to "go to the moon and bring back a stone within an hour," and then you lie on the beach, basking in the sun and drinking juice, and an hour later your subordinate will come back to you with a stone from the moon. This kind of behavior of completing the task immediately without any conditions and without requiring you to care about any details is a sign of high cohesion.

When it comes to interfaces, try to refine your interfaces. The interface is a commitment to the outside world, the less commitment, the more beneficial to the development of the system, the less the risk of change, but also helps to reduce costs.

At this point, I believe you have a deeper understanding of "how to implement the Java object-oriented interface isolation principle". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report