一、基本介绍
一个类对另一个类的依赖应该建立在最小的接口上
二、应用实例
分析以上类图:
首先有四个类,分别是A,B,C,D,以及一个接口Interface1,其中有五个抽象方法,分别是operation1,operation2,operation3,operation4,operation5
类B和类D都会实现Interface1接口
类A通过Interface1接口依赖类B,但是类A中只会使用到接口的operation1,operation2,operation3三个方法
类C通过Interface1接口依赖类D,但是类C中只会使用到接口的operation1,operation4,operation5三个方法
编写程序如下:
1 public classSegregation1 { 2 public static voidmain(String[] args) { 3 A a = newA(); 4 //类A通过接口依赖类B 5 a.depend1(newB()); 6 a.depend2(newB()); 7 a.depend3(newB()); 8 9 C c = newC(); 10 //类C通过接口依赖类D 11 c.depend1(newD()); 12 c.depend4(newD()); 13 c.depend5(newD()); 14 } 15 } 16 17 interfaceInterface1 { 18 voidoperation1(); 19 voidoperation2(); 20 voidoperation3(); 21 voidoperation4(); 22 voidoperation5(); 23 } 24 25 class B implementsInterface1 { 26 @Override 27 public voidoperation1() { 28 System.out.println("B中实现了operation1"); 29 } 30 @Override 31 public voidoperation2() { 32 System.out.println("B中实现了operation2"); 33 } 34 @Override 35 public voidoperation3() { 36 System.out.println("B中实现了operation3"); 37 } 38 @Override 39 public voidoperation4() { 40 System.out.println("B中实现了operation4"); 41 } 42 @Override 43 public voidoperation5() { 44 System.out.println("B中实现了operation5"); 45 } 46 } 47 48 class D implementsInterface1 { 49 @Override 50 public voidoperation1() { 51 System.out.println("D中实现了operation1"); 52 } 53 @Override 54 public voidoperation2() { 55 System.out.println("D中实现了operation2"); 56 } 57 @Override 58 public voidoperation3() { 59 System.out.println("D中实现了operation3"); 60 } 61 @Override 62 public voidoperation4() { 63 System.out.println("D中实现了operation4"); 64 } 65 @Override 66 public voidoperation5() { 67 System.out.println("D中实现了operation5"); 68 } 69 } 70 71 //类A通过接口Interface1依赖类B,但是只会用到1,2,3方法 72 classA { 73 public voiddepend1(Interface1 interface1) { 74 interface1.operation1(); 75 } 76 public voiddepend2(Interface1 interface1) { 77 interface1.operation2(); 78 } 79 public voiddepend3(Interface1 interface1) { 80 interface1.operation3(); 81 } 82 } 83 84 //类C通过接口Interface1依赖类B,但是只会用到1,4,5方法 85 classC { 86 public voiddepend1(Interface1 interface1) { 87 interface1.operation1(); 88 } 89 public voiddepend4(Interface1 interface1) { 90 interface1.operation4(); 91 } 92 public voiddepend5(Interface1 interface1) { 93 interface1.operation5(); 94 } 95 }
输出结果:
类A和类C都通过Interface1接口分别依赖类B和类D
但是由于类A和类C只需要使用Interface1接口中的部分方法,造成了类B和类D中方法的冗余
类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法
将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
接口Interface1中出现的方法,根据实际情况拆分为三个接口
解决方案:将Interface1接口的粒度进一步细化,分解为Interface1,Interface2,Interface3三个接口
代码实现:
1 public classSegregation1 { 2 public static voidmain(String[] args) { 3 A a = newA(); 4 //类A通过接口依赖类B 5 a.depend1(newB()); 6 a.depend2(newB()); 7 a.depend3(newB()); 8 9 C c = newC(); 10 //类C通过接口依赖类D 11 c.depend1(newD()); 12 c.depend4(newD()); 13 c.depend5(newD()); 14 } 15 } 16 17 interfaceInterface1 { 18 voidoperation1(); 19 } 20 21 interfaceInterface2 { 22 voidoperation2(); 23 24 voidoperation3(); 25 } 26 27 interfaceInterface3 { 28 voidoperation4(); 29 30 voidoperation5(); 31 } 32 33 class B implementsInterface1, Interface2 { 34 @Override 35 public voidoperation1() { 36 System.out.println("B中实现了operation1"); 37 } 38 39 @Override 40 public voidoperation2() { 41 System.out.println("B中实现了operation2"); 42 } 43 44 @Override 45 public voidoperation3() { 46 System.out.println("B中实现了operation3"); 47 } 48 } 49 50 class D implementsInterface1, Interface3 { 51 @Override 52 public voidoperation1() { 53 System.out.println("D中实现了operation1"); 54 } 55 56 @Override 57 public voidoperation4() { 58 System.out.println("D中实现了operation4"); 59 } 60 61 @Override 62 public voidoperation5() { 63 System.out.println("D中实现了operation5"); 64 } 65 } 66 67 //类A通过接口Interface1,2依赖类B 68 classA { 69 public voiddepend1(Interface1 interface1) { 70 interface1.operation1(); 71 } 72 73 public voiddepend2(Interface2 interface2) { 74 interface2.operation2(); 75 } 76 77 public voiddepend3(Interface2 interface2) { 78 interface2.operation3(); 79 } 80 } 81 82 //类C通过接口Interface1,3依赖类B 83 classC { 84 public voiddepend1(Interface1 interface1) { 85 interface1.operation1(); 86 } 87 88 public voiddepend4(Interface3 interface3) { 89 interface3.operation4(); 90 } 91 92 public voiddepend5(Interface3 interface3) { 93 interface3.operation5(); 94 } 95 }
输出结果:
与实现接口隔离原则之前的输出结果相同,但是在接口的粒度上进行了细化,降低了代码的耦合度