概述
定义:客户端不应该依赖它不需要的接口,一个类对另一个类的依赖应该建立在最小的接口上。简单地说,就是不要定义方法多而全的总接口,尽量细化接口,接口中的方法尽量少。
案例演示:
类C通过通过接口依赖(使用)类A,但是只会使用到1、2、3方法,类D通过接口依赖(使用)类B,但是只会使用到1、4、5方法
未使用接口隔离原则的设计
代码:
public interface TotalInterface {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
public class TotalInterfaceImplA implements TotalInterface {
@Override
public void operation1() {
System.out.println("类A实现了接口的方法1");
}
@Override
public void operation2() {
System.out.println("类A实现了接口的方法2");
}
@Override
public void operation3() {
System.out.println("类A实现了接口的方法3");
}
@Override
public void operation4() {
System.out.println("类A实现了接口的方法4");
}
@Override
public void operation5() {
System.out.println("类A实现了接口的方法5");
}
}
public class TotalInterfaceImplB implements TotalInterface {
@Override
public void operation1() {
System.out.println("类B实现了接口的方法1");
}
@Override
public void operation2() {
System.out.println("类B实现了接口的方法2");
}
@Override
public void operation3() {
System.out.println("类B实现了接口的方法3");
}
@Override
public void operation4() {
System.out.println("类B实现了接口的方法4");
}
@Override
public void operation5() {
System.out.println("类B实现了接口的方法5");
}
}
public class C {
public void depend1(TotalInterface t) {
t.operation1();
}
public void depend2(TotalInterface t) {
t.operation2();
}
public void depend3(TotalInterface t) {
t.operation3();
}
}
public class D {
public void depend1(TotalInterface t) {
t.operation1();
}
public void depend4(TotalInterface t) {
t.operation4();
}
public void depend5(TotalInterface t) {
t.operation5();
}
}
public class SegregationTest {
public static void main(String[] args) {
C c = new C();
c.depend1(new TotalInterfaceImplA());
c.depend2(new TotalInterfaceImplA());
c.depend3(new TotalInterfaceImplA());
D d = new D();
d.depend1(new TotalInterfaceImplB());
d.depend4(new TotalInterfaceImplB());
d.depend5(new TotalInterfaceImplB());
}
}
上述代码中的问题:类C通过通过接口依赖(使用)类A,但是只会使用到1、2、3方法,类D通过接口依赖(使用)类B,但是只会使用到1、4、5方法,如果接口TotalInterface对类C和类D来说不是最小接口,那么类A和类B必须实现他们不需要的方法。
使用接口隔离原则的设计
将接口TotalInterface拆分为独立的几个接口,类C和类D分别与他们需要的接口建立依赖关系,也就是采用接口隔离原则。
接口TotalInterface中出现的方法,根据实际情况拆分成三个接口:
代码:
public interface Interface1 {
void operation1();
}
public interface Interface2 {
void operation2();
void operation3();
}
public interface Interface3 {
void operation4();
void operation5();
}
public class ImplA implements Interface1, Interface2 {
@Override
public void operation1() {
System.out.println("类A实现了接口1的方法1");
}
@Override
public void operation2() {
System.out.println("类A实现了接口2的方法2");
}
@Override
public void operation3() {
System.out.println("类A实现了接口2的方法3");
}
}
public class ImplB implements Interface1, Interface3 {
@Override
public void operation1() {
System.out.println("类B实现了接口1的方法1");
}
@Override
public void operation4() {
System.out.println("类B实现了接口3的方法4");
}
@Override
public void operation5() {
System.out.println("类B实现了接口3的方法5");
}
}
//类C通过接口Interface1、Interface2依赖(使用)类A
public class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
//类D通过接口Interface1、Interface3依赖(使用)类B
public class D {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
public class SegregationTest {
public static void main(String[] args) {
C c = new C();
c.depend1(new ImplA());
c.depend2(new ImplA());
c.depend3(new ImplA());
D d = new D();
d.depend1(new ImplB());
d.depend4(new ImplB());
d.depend5(new ImplB());
}
}
注意:
适度使用原则,如果接口设计的过小,里面的方法过少,则会造成接口的数量过多,使程序设计的复杂性增加。