接口隔离原则
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上
考虑以上关系,其中,A只调用Interface1的operation1、operation2和operation3,B只调用Interface1的operation1 operation4和operation5,
public interface Interface1 {
public void operation1();
public void operation2();
public void operation3();
public void operation4();
public void operation5();
}
public class A {
public void method1(Interface1 i1) {
i1.operation1();
}
public void method2(Interface1 i1) {
i1.operation2();
}
public void method3(Interface1 i1) {
i1.operation3();
}
}
public class B {
public void method1(Interface1 i1) {
i1.operation1();
}
public void method2(Interface1 i1) {
i1.operation4();
}
public void method3(Interface1 i1) {
i1.operation5();
}
}
public class C implements Interface1 {
@Override
public void operation1() {
}
@Override
public void operation2() {
}
@Override
public void operation3() {
}
@Override
public void operation4() {
}
@Override
public void operation5() {
}
}
public class D implements Interface1 {
@Override
public void operation1() {
}
@Override
public void operation2() {
}
@Override
public void operation3() {
}
@Override
public void operation4() {
}
@Override
public void operation5() {
}
}
这样,接口A与B就依赖了接口中相应的不需要的方法,违反了接口隔离原则,因此要对接口Interface1进行拆分