基本介绍
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
应用实例
如上图所示,假设,类A通过Interface1会依赖类B,但是类A只会使用到 接口的1,2,3三个方法;类C通过Interface1会依赖类D,但是C中只会用到接口的1,4,5三个方法。
上述假设实例用代码实现如下:
public interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
@Override
public void operation1() {
System.out.println("B中实现了operation1");
}
@Override
public void operation2() {
System.out.println("B中实现了operation2");
}
@Override
public void operation3() {
System.out.println("B中实现了operation3");
}
@Override
public void operation4() {
System.out.println("B中实现了operation4");
}
@Override
public void operation5() {
System.out.println("B中实现了operation5");
}
}
class D implements Interface1 {
@Override
public void operation1() {
System.out.println("D中实现了operation1");
}
@Override
public void operation2() {
System.out.println("D中实现了operation2");
}
@Override
public void operation3() {
System.out.println("D中实现了operation3");
}
@Override
public void operation4() {
System.out.println("D中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D中实现了operation5");
}
}
// 类A通过接口interface1依赖B类
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
// 类C通过接口interface1依赖D类
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
// 主方法调用
public static void main(String[] args) {
A a = new A();
B b = new B();
a.depend1(b);
a.depend2(b);
a.depend3(b);
C c = new C();
D d = new D();
c.depend1(d);
c.depend4(d);
c.depend5(d);
}
上述实例中,存在一个问题:类A通过接口依赖类B,类C通过接口依赖类D,如果接口对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法,如:类B实现了类A不需要的4,5方法,类D实现了类C不需要的2,3方法。
对于上述方法的改进方法:
将接口Interface1拆分为几个独立的接口,类A和类C分别与他们需要的接口建立依赖关系。即采用接口隔离原则。如下图所示:
代码实现如下:
public interface Interface1 {
public void operation1();
}
public interface Interface2 {
public void operation2();
public void operation3();
}
public interface Interface3 {
public void operation4();
public void operation5();
}
class B implements Interface1, Interface2 {
@Override
public void operation1() {
System.out.println("B中实现了operation1");
}
@Override
public void operation2() {
System.out.println("B中实现了operation2");
}
@Override
public void operation3() {
System.out.println("B中实现了operation3");
}
}
class D implements Interface1, Interface3 {
@Override
public void operation1() {
System.out.println("D中实现了operation1");
}
@Override
public void operation4() {
System.out.println("D中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D中实现了operation5");
}
}
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
// 主方法调用
public static void main(String[] args) {
A a = new A();
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D());
c.depend4(new D());
c.depend5(new D());
}
上述改进方法满足了一个类对另一个类的依赖建立在最小的接口上,即满足了接口隔离原则。