基本介绍
客户端不应该依赖它不需要的接口,一个类对另一个类的依赖应该建立在最小接口上。
场景
类A通过interface依赖类B,类C通过interface依赖类D。如果interface对于类A和类C来说不是最小接口,那么类B和类D就需要实现他们不需要的接口。应该将interface按照最小接口拆分成独立的接口,类A和类C分别与他们需要的接口建立依赖关系。
代码示例
示例一(错误写法)
接口未进行隔离,类B和类C需要实现interface的全部接口,即使有些接口未用到也需要实现。
// 接口
interface Interface {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface {
public void operation1() {
System.out.println("B 实现了 operation1");
}
public void operation2() {
System.out.println("B 实现了 operation2");
}
public void operation3() {
System.out.println("B 实现了 operation3");
}
public void operation4() {
System.out.println("B 实现了 operation4");
}
public void operation5() {
System.out.println("B 实现了 operation5");
}
}
class D implements Interface {
public void operation1() {
System.out.println("D 实现了 operation1");
}
public void operation2() {
System.out.println("D 实现了 operation2");
}
public void operation3() {
System.out.println("D 实现了 operation3");
}
public void operation4() {
System.out.println("D 实现了 operation4");
}
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
// A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
class A {
public void depend1(Interface i) {
i.operation1();
}
public void depend2(Interface i) {
i.operation2();
}
public void depend3(Interface i) {
i.operation3();
}
}
// C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
class C {
public void depend1(Interface i) {
i.operation1();
}
public void depend4(Interface i) {
i.operation4();
}
public void depend5(Interface i) {
i.operation5();
}
}
示例二(接口隔离)
将interface拆分成几个独立的的接口,类A和类C只与他们需要的接口建立依赖关系,采用接口隔离原则,将接口种的方法根据实际情况进行拆分。
// 接口1
interface Interface1 {
void operation1();
}
// 接口2
interface Interface2 {
void operation2();
void operation3();
}
// 接口3
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1, Interface2 {
public void operation1() {
System.out.println("B 实现了 operation1");
}
public void operation2() {
System.out.println("B 实现了 operation2");
}
public void operation3() {
System.out.println("B 实现了 operation3");
}
}
class D implements Interface1, Interface3 {
public void operation1() {
System.out.println("D 实现了 operation1");
}
public void operation4() {
System.out.println("D 实现了 operation4");
}
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
// A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
class A {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
// C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
class C {
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
总结
- 接口尽量小,但是要有限度。对接口进行细化可以提高程序设计灵活性是不挣的事实,但是如果过小,则会造成接口数量过多, 使设计复杂化。所以一定要适度。
- 为依赖接口的类定制服务,只暴露给调用的类它需要的方法, 它不需要的方法则隐藏起来。只有专注地为一个模块提供定制服务, 才能建立最小的依赖关系。
- 提高内聚,减少对外交互。使接口用最少的方法去完成最多的事情。
- 运用接口隔离原则,一定要适度,接口设计的过大或过小都不好。设计接口的时候,只有多花些时间去思考和筹划,才能准确地实践这一原则。
网友评论