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

What is the principle of interface isolation in Java

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the principle of interface isolation in Java is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. What is the principle of interface isolation?

The client should not rely on interfaces it does not need, that is, the dependency of one class on another should be based on the smallest range of interfaces.

two。 Corresponding code

The picture above violates the principle of interface isolation. The corresponding code is as follows:?

Package com.szh.principle.segregation; / * * * / interface Interface1 {void operation1 (); void operation2 (); void operation3 (); void operation4 (); void operation5 ();} class B implements Interface1 {public void operation1 () {System.out.println ("B implements operation1");} public void operation2 () {System.out.println ("B implements operation2") } public void operation3 () {System.out.println ("B implements operation3");} public void operation4 () {System.out.println ("B implements operation4");} public void operation5 () {System.out.println ("B implements operation5");}} class D implements Interface1 {public void operation1 () {System.out.println ("D implements operation1") } public void operation2 () {System.out.println ("D implements operation2");} public void operation3 () {System.out.println ("D implements operation3");} public void operation4 () {System.out.println ("D implements operation4");} public void operation5 () {System.out.println ("D implements operation5") }} class A {/ / A class depends on (uses) class B through the interface Interface1, but only uses 1meme, Interface1 I, method public void depend1 (Interface1 I) {i.operation1 ();} public void depend2 (Interface1 I) {i.operation2 ();} public void depend3 (Interface1 I) {i.operation3 () }} class C {/ / C class depends on (uses) class D through the interface Interface1, but only uses 1, Interface1, 4, 5 methods public void depend1 (Interface1 I) {i.operation1 ();} public void depend4 (Interface1 I) {i.operation4 ();} public void depend5 (Interface1 I) {i.operation5 () }} public class Segregation1 {public static void main (String [] args) {AA = new A (); a.depend1 (new B ()); / / Class A depends on class B a.depend2 (new B ()); a.depend3 (new B ()); C c = new C (); c.depend1 (new D ()) / / Class C depends on (uses) Class D c.depend4 (new D ()); c.depend5 (new D ());}} through the interface

The code is long, but it is not difficult to understand. Class A depends on class B, but only uses methods 1, 2, and 3 in the top-level interface. While class C depends on class D, but only uses the methods 1, 4 and 5 of the top-level interface, that is to say, at the level of An and B, it has nothing to do with the 4 and 5 methods of the top-level interface. then class B does not need to rewrite the 4 and 5 methods when implementing the top-level interface. But there is a problem here is that the top-level interface includes the five methods 1 to 5. If you implement this interface, you have to rewrite these five methods, then we can consider splitting the top-level interface into multiple interfaces. Implement whatever you need, which is the so-called interface isolation.

3. Improve the code

After the above description, we can rewrite the code in the following form.

That is, the top-level interface is divided into three small interfaces, and the B and D classes will implement the interface according to the actual situation (because the five methods have been separated).

Package com.szh.principle.segregation.improve; / * * * / interface Interface1 {void operation1 ();} interface Interface2 {void operation2 (); void operation3 ();} interface Interface3 {void operation4 (); void operation5 ();} class B implements Interface1, Interface2 {public void operation1 () {System.out.println ("B implements operation1") } public void operation2 () {System.out.println ("B implements operation2");} public void operation3 () {System.out.println ("B implements operation3");}} class D implements Interface1, Interface3 {public void operation1 () {System.out.println ("D implements operation1");} public void operation4 () {System.out.println ("D implements operation4") } public void operation5 () {System.out.println ("D implements operation5");}} class A {/ / A depends on (uses) class B through the interface Interface1,Interface2, but only uses public void depend1 (Interface1 I) {i.operation1 ();} public void depend2 (Interface2 I) {i.operation2 () } public void depend3 (Interface2 I) {i.operation3 ();}} class C {/ / C depends on (uses) class D through the interface Interface1,Interface3, but only uses public void depend1 (Interface1 I) {i.operation1 ();} public void depend4 (Interface3 I) {i.operation4 ();} public void depend5 (Interface3 I) {i.operation5 () }} public class Segregation2 {public static void main (String [] args) {AA = new A (); a.depend1 (new B ()); / / Class A depends on class B a.depend2 (new B ()); a.depend3 (new B ()); C c = new C (); c.depend1 (new D ()) / / Class C depends on (uses) Class D c.depend4 (new D ()); c.depend5 (new D ());}} through the interface

4. Summary of interface isolation principles

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

The interface Interface1 is divided into several independent interfaces, and class An and class C respectively establish dependencies with the interfaces they need. That is, the principle of interface isolation is adopted.

The above is all the content of the article "what is the principle of Interface isolation in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report