客户端不应该依赖它不需要的接口,既一个类对另一个类的依赖应该建立在最小的接口上
举个例子1
上图1中,类B实现了接口Interface1; 类D实现了接口Interface1; 类A通过接口Interface1分别依赖类B,但只使用接口的1,2,3三个方法; 类C通过接口Interface1分别依赖类D,但只使用接口的1,4,5三个方法;
public class Segregation1{
public static void main(String[] args) {
Interface1 interface1 = new B();
Interface1 interface2 = new D();
A a = new A();
a.dependB1(interface1);
a.dependB2(interface1);
a.dependB3(interface1);
C c = new C();
c.dependD1(interface2);
c.dependD4(interface2);
c.dependD5(interface2);
}
}
// 接口Interface1,定义5个方法
interface Interface1{
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
// 类B继承接口Interface1,实现接口定义的所有方法
class B implements Interface1{
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");
}
}
//类D继承接口Interface1,实现接口定义的所有方法
class D implements Interface1 {
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 dependB1(Interface1 interface1){
interface1.operation1();
}
public void dependB2(Interface1 interface1){
interface1.operation2();
}
public void dependB3(Interface1 interface1){
interface1.operation3();
}
}
// 类C 通过接口interface1依赖B类,但只使用方法1,4,5
class C{
public void dependD1(Interface1 interface1){
interface1.operation1();
}
public void dependD4(Interface1 interface1){
interface1.operation4();
}
public void dependD5(Interface1 interface1){
interface1.operation5();
}
}
分析: 类A通过接口Interface1依赖B,类C通过接口Interface1依赖D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D就不必去实现它们不需要的方法
按照接口隔离原则应当这样处理: 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
举个例子2
例子2使用接口隔离原则改进: 1)将接口Interface1拆分为独立的三个接口,类A和类C通过接口隔离原则分别与他们建立依赖关系
public class Segregation2 {
public static void main(String[] args) {
A a = new A();
a.dependB1(new B());
a.dependB2(new B());
a.dependB3(new B());
C c = new C();
c.dependD1(new D());
c.dependD4(new D());
c.dependD5(new D());
}
}
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
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");
}
}
class A {
public void dependB1(Interface1 interface1) {
interface1.operation1();
}
public void dependB2(Interface2 interface2) {
interface2.operation2();
}
public void dependB3(Interface2 interface2) {
interface2.operation3();
}
}
class C {
public void dependD1(Interface1 interface1) {
interface1.operation1();
}
public void dependD4(Interface3 interface3) {
interface3.operation4();
}
public void dependD5(Interface3 interface3) {
interface3.operation5();
}
}
// 运行结果
B实现了operation1
B实现了operation2
B实现了operation3
D实现了operation1
D实现了operation4
D实现了operation5