1, define,

As usual, let’s first look at the definition of the interface isolation principle.

Dependencies between classes should be established on the smallest interface.

The interface should have as few methods as possible, not too many bloated interfaces, and not too many unrelated logical methods.

It’s kind of like the single responsibility principle, it’s all about keeping the functions as simple as possible, and not having too many redundant things that aren’t relevant.

The single responsibility principle is primarily about classes and methods, whereas the interface isolation principle is about interfaces.

2,

The little kitchen washes the vegetables and the chef cooks the food.

In the kitchen of tomato restaurant, the boss and the desperate chef are chatting.

V: Our tomato restaurant has been getting a lot of good reviews lately. The dishes are delicious, thanks to you, the chef.

Chef: Yes, thank me for that.

Boss: Huh? Are you sure?

Chef: Not yet, not yet, thanks to me… Our hao boss, this time confirmed. (cold sweat)

Boss: You are so desperate for life. It’s amazing. But now, with the increase of customers, there is not enough staff again, and it is definitely too late to recruit a chef. Do you have any good ideas?

Chef: I do have one. We can hire a little cook.

Boss: But the cook is not delicious enough. It’s easy to lose customers.

Chef: The little chef doesn’t cook. The little chef only washes dishes. In this way, the chef doesn’t have to do the washing, he just does the cooking, so efficiency goes up.

Boss: Don’t you want to wash the vegetables?

Chef: Of course not. I’m just, just, just thinking about the company.

Boss: Ok, that’s right. Let’s hire.

3, code,

Previously we talked about programming interfaces in dependency inversion, so first let’s define a chef interface.

package com.fanqiekt.principle.segregation; /** * wash(); /** * wash(); /** * wash(); /** ** / void cook (); }Copy the code

A chef does two things, one is to wash vegetables and the other is to cook them.

Next, let’s write the code for Chef, who implements the chef interface.

The chef cooks the food, but he doesn’t wash it.

package com.fanqiekt.principle.segregation; /** * public class BigChef implements IChef {/** * public void wash() {} @override public void cooking() {system.out.println (" cook cook "); }}Copy the code

Let’s do the little kitchen part again, the little kitchen also implements the chef interface.

The little cook doesn’t cook, the little cook only washes.

package com.fanqiekt.principle.segregation; /** * @author */ public class Kitchen implements IChef {@override public void wash() { System.out.println("小 kitchen wash dishes "); } @override public void cooking() {}}Copy the code

How about writing it this way?

Of course not, each class has redundant methods that are not relevant to it. For example, wash method in BigChef, cooking method in Kitchen.

How does this phenomenon come about?

The interface is not minimized enough. The interface isolation principle is designed to solve this problem.

We can write it as two interfaces, one for cooking and one for washing vegetables.

package com.fanqiekt.principle.segregation; /** ** public interface ICook {/** ** ** / void cooking(); }Copy the code

Interface for cooking.

package com.fanqiekt.principle.segregation; /** * wash(); /** * wash(); /** * wash(); }Copy the code

Interface for washing vegetables.

Let’s look at a concrete implementation class that complies with the interface isolation principle.

package com.fanqiekt.principle.segregation; /** ** @author */ public class BigChef implements ICook{/** ** */ @override public void cooking() {system.out.println (" cook "); }}Copy the code

So there’s no redundant code.

package com.fanqiekt.principle.segregation; /** ** @author */ public class Kitchen implements IWash {/** * */ @override public void wash() {system.out.println (" wash "); / / Override public void wash() {system.out.println (" wash "); }}Copy the code

The kitchen also has no redundant code.

Isn’t it more elegant to write it this way?

If there is a new type of cook that can both wash and cook, then it is ok to implement ICook and IWash interface at the same time.

3, strengths,

It is similar to the single responsibility principle and has similar advantages.

Risk mitigation Modifying one of the services does not affect the business.

Easy maintenance easy extension Without redundant code, interfaces that comply with the principle of interface isolation are easier to extend and maintain.

Hip-hop said

Next, please enjoy the original song of interface Isolation Principles.

Hip Hop says: Interface segregation Principle Lazy people have worked hard for many years to become a chef, so don't care about washing vegetables. They can't pick the trivial things like washing vegetables, but the dedicated chefs can't cook big dishes. Because these can't come, so there are too many interface functions So that the structure is appropriate, the risk is reduced, the maintenance is easy and there is a pattern to use it is absolutely reasonableCopy the code

To try it, click here

Idle no matter to listen to music, knowledge has filled the brain to;

Learning new ways to review, wearing headphones is a big deal.