标签:ati https pack 介绍 隔离 ide 依赖 string 关系
接口隔离原则(Interface Segregation Principle),又称ISP原则
1、 客户端不应该依赖它不需要的接口
2、 类间的依赖关系应该建立在最小的接口上
通俗的来讲,不要在一个接口中定义多个方法,接口应该尽量细化
package jiekogeli;
public class SegregationDemo {
public static void main(String[] args) {
new FruitShop().cutApple(new CookZhang());
new FruitShop().cutTomato(new CookZhang());
new VegetableShop().cutTomato(new CookLi());
new VegetableShop().cutPotato(new CookLi());
}
}
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();
}
}
标签:ati https pack 介绍 隔离 ide 依赖 string 关系
原文地址:https://www.cnblogs.com/stu-jyj3621/p/14667837.html