-
接口隔离原则 Interface Segregation Principle
-
一、定义
-
1、客户端不应该依赖它不需要的接口(一个接口中的方法不应该冗余)Clients should not be forced to depend upon interfaces that they don’t use.
2、类间的依赖关系应该建立在最小的接口上,The dependency of one class to another one should depend on the smallest possible interface.
- 我们要为各个类建立专用的接口,而不要试图去建立一个很庞大的接口供所有依赖它的类去调用。
-
例如下面
错误的示范是为了人类和鸟类,创建了一个通用的行为接口,Behavior接口。 -
interface Behavior { void fly(); void running(); void wave(); }
正确的示范是分别拆分行为中专属于鸟类的,专属于人类的,还有共有的,分成三个接口。
interface BehaviorFly {
void fly();
}
interface BehaviorRun {
void running();
}
interface BehaviorWave {
void wave();
}
代码图