接口隔离原则(Interface Segregation Principe),又称为ISP原则,官方定义为:java
一、Clients should not be forced to depend upon interfaces that they don't use.ide
客户端不该该依赖它不须要的接口ui
二、The dependency of one class to another one should depend on the smallest possible interface设计
类间的依赖关系应该创建在最小的接口上code
通俗的来说,不要在一个接口里面定义过多的方法,接口应该最细化接口
假设有这样一个案例场景,如今有一个接口knife,给定他有三个能力,能够切苹果,切番茄,切土豆,两个类张厨师,李厨师分别具备这些能力,有一个水果店类,假设是须要张师傅来切苹果和切番茄,而另外一个蔬菜店类须要李师傅来切番茄和切土豆ip
//定义接口knife interface knife{ //切苹果的能力 void cutApple(); //切番茄的能力 void cutTomato(); //切土豆的能力 void cutPotato(); } //张厨师类 class CookZhang implements Knife{ @Override public void cutApple(){ System.out.println("张厨师正在切水果"); } @Override public void cutTomato(){ System.out.println("张厨师正在切番茄"); } @Override public void cutPotato(){ System.out.println("张厨师正在切土豆"); } } //李厨师类 class CookLi implements Knife{ @Override public void cutApple(){ System.out.println("李厨师正在切水果"); } @Override public void cutTomato(){ System.out.println("李厨师正在切番茄"); } @Override public void cutPotato(){ System.out.println("李厨师正在切土豆"); } } //水果店类 class FruitShop{ //定义方法 public void cutApple(Knife knife){ knife.cutApple(); } public void cutTomato(Knife knife){ knife.cutTomato(); } } //蔬菜店类 class VegetableShop{ //定义方法 public void cutPotato(Knife knife){ knife.cutPotato(); } public void cutTomato(Knife knife){ knife.cutTomato(); } } public class SegregationDemo{ public static void main(Stringp[] agrs){ //水果店类 new FruitShop.cutApple(new CookZhang()); new FruitShop.cutTomato(new CookZhang()); //蔬菜店类 new VegetableShop.cutTomato(new CookLi()); new VegetableShop.cutPotato(new CookLi()); } }
上面的写法违反了接口隔离的原则,张师傅并不须要切土豆,李厨师也不须要切苹果ci
违反了类间的依赖关系应该创建在最小的接口上这个原则get
//将揭开kinfe拆分红三个独立的接口 interface AppleKnife{ //切苹果的能力 void cutApple(); } interface TomatoKnife{ //切番茄的能力 void cutTomato(); } interface PotatoKnife{ //切土豆的能力 void cutPotato(); } //张厨师类 class CookZhang implements AppleKnife,TomatoKnife{ @Override public void cutApple(){ System.out.println("张厨师正在切水果"); } @Override public void cutTomato(){ System.out.println("张厨师正在切番茄"); } } //李厨师类 class CookLi implements TomatoKnife,PotatoKnife{ @Override public void cutTomato(){ System.out.println("李厨师正在切番茄"); } @Override public void cutPotato(){ System.out.println("李厨师正在切土豆"); } } //水果店类 class FruitShop{ //定义方法 public void cutApple(AppleKnife knife){ knife.cutApple(); } public void cutTomato(TomatoKnife knife){ knife.cutTomato(); } } //蔬菜店类 class VegetableShop{ //定义方法 public void cutPotato(PotatoKnife knife){ knife.cutPotato(); } public void cutTomato(TomatoKnife knife){ knife.cutTomato(); } } public class SegregationDemo{ public static void main(Stringp[] agrs){ //水果店类 new FruitShop.cutApple(new CookZhang()); new FruitShop.cutTomato(new CookZhang()); //蔬菜店类 new VegetableShop.cutTomato(new CookLi()); new VegetableShop.cutPotato(new CookLi()); } }
接口隔离原则就是当我一个类经过接口依赖(使用)另外一个类的时候,要保证依赖的该接口是最小的,接口里面用不到的,就进行隔离,而隔离的作法就是,就对原来的接口进行拆分,拆分为最小粒度,来避免耦合。it
单一职责原则:合理的职责分解,一个类只负责一项职责
接口隔离原则:类间的依赖关系应该创建在最小的接口上
相同点
都要求对结构进行拆分,都要求更小的粒度,都要求减小耦合
不一样点
审视的角度不一样
单一职责原则:类与接口职责单一,注重的是职责
接口隔离原则:要求咱们尽可能使用多个专门的接口,注重的是接口的设计
咱们使用接口隔离原则进行接口拆分的时候,要遵循单一职责原则