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

Case Analysis of Java Interface isolation principle

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

Share

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

Most people do not understand the knowledge points of this article "case Analysis of Java Interface isolation principle", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java Interface isolation principle case Analysis" article.

Define

Interface isolation principle (Interface Segregation Principle), also known as ISP principle, is officially defined as

1. The client should not rely on interfaces it does not need

two。 Dependencies between classes should be based on the smallest interface

To put it simply, do not define too many methods in a class, and the interface should be as simple and detailed as possible.

Case requirement

Suppose there is such a case scenario, there is now an interface Repair, given that he has three abilities, he can repair cars, trains, airplanes, two realization classes, Master Zhang and Master Li have these capabilities respectively, and there is a 4S shop, assuming that Master Zhang is needed to repair cars and airplanes, while another 4s store requires Master Li to repair cars and trains.

Option one

Define the maintenance interface class Repair.java

/ * author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public interface Repair {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:03 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void car () / * repair trains * @ author: liyajie * @ date: 17:03 on 2022-1-29 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void train () / * author: liyajie * @ date: 17:03 on 2022-1-29 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void air ();}

Maintenance of Master Li RepairLi.java

/ * * maintenance class * @ author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public class RepairLi implements Repair {@ Override public void car () {System.out.println ("Master Li repairs cars");} @ Override public void train () {System.out.println ("Master Li repairs trains") } @ Override public void air () {System.out.println ("Master Li fix the plane");}}

Maintenance master class RepairWang.java

/ * * maintenance class * @ author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public class RepairWang implements Repair {@ Override public void car () {System.out.println ("Master Wang repairs cars");} @ Override public void train () {System.out.println ("Master Wang repairs trains") } @ Override public void air () {System.out.println ("Master Wang repairs the plane");}}

4s shop 1

/ * 4s store 1 * @ author:liyajie * @ createTime:2022/1/29 17:07 * @ version:1.0 * / public class S1Shop {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:10 * @ param repair * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void car (Repair repair) {repair.car () Author: liyajie * @ date: 17:10 on 2022-1-29 * @ param repair * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void train (Repair repair) {repair.train ();}}

4s Shop 2

/ * 4s store 2 * @ author:liyajie * @ createTime:2022/1/29 17:07 * @ version:1.0 * / public class S2Shop {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:10 * @ param repair * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void car (Repair repair) {repair.car () Author: liyajie * @ date: 17:10 on 2022-1-29 * @ param repair * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void air (Repair repair) {repair.air ();}}

Test Class 1

/ * Test class 1 * @ author:liyajie * @ createTime:2022/1/29 16:46 * @ version:1.0 * / public class Test1 {public static void main (String [] args) {/ / 4s store 1 new S1Shop () .car (new RepairLi ()); new S1Shop () .train (new RepairLi ()); / / 4s store 2 new S2Shop () .car (new RepairWang ()) New S2Shop (). Air (new RepairWang ());}} scenario two

Define the interface for vehicle maintenance

/ * author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public interface RepairCar {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:03 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void car ();}

Define the type of maintenance train

/ * author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public interface RepairTrain {/ * repair trains * @ author:liyajie * @ date: 2022-1-29 17:03 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void train ();}

Define the class of maintenance aircraft

/ * author:liyajie * @ createTime:2022/1/29 17:00 * @ version:1.0 * / public interface RepairAir {/ * maintenance aircraft * @ author:liyajie * @ date: 2022-1-29 17:03 * @ param * @ return void * @ exception: * @ update: * @ updatePerson: * * / void air ();}

4s shop 1

/ * 4s store 1 * @ author:liyajie * @ createTime:2022/1/29 17:07 * @ version:1.0 * / public class S1ShopNew {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:10 * @ param repairCar * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void car (RepairCar repairCar) {repairCar.car () Author: liyajie * @ date: 17:10 on 2022-1-29 * @ param repairTrain * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void train (RepairTrain repairTrain) {repairTrain.train ();}}

4s Shop 2

/ * 4s store 2 * @ author:liyajie * @ createTime:2022/1/29 17:07 * @ version:1.0 * / public class S2ShopNew {/ * repair cars * @ author:liyajie * @ date: 2022-1-29 17:10 * @ param repairCar * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void car (RepairCar repairCar) {repairCar.car () Author: liyajie * @ date: 17:10 on 2022-1-29 * @ param repairAir * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void air (RepairAir repairAir) {repairAir.air ();}}

Test class

/ * Test class 1 * @ author:liyajie * @ createTime:2022/1/29 16:46 * @ version:1.0 * / public class Test2 {public static void main (String [] args) {/ / 4s store 1 new S1ShopNew () .car (new RepairLiNew ()); new S1ShopNew () .train (new RepairLiNew ()); / / 4s store 2 new S2ShopNew () .car (new RepairWangNew ()) Comparative analysis of new S2ShopNew (). Air (new RepairWangNew ());}}

Plan one violates the principle of interface isolation, because both Master Li and Master Wang have realized three methods of maintaining the interface: repairing cars, trains and airplanes. But we don't need so much in the demand. We only need Master Li to fix cars and trains, and Master Wang to fix cars and airplanes. The dependency relationship is not based on the smallest interface.

The second scheme abides by the principle of interface isolation, isolates the interface of Master Li and Master Wang, implements their own two methods, and avoids coupling.

The above is the content of this article on "case Analysis of Java Interface isolation principle". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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: 220

*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