基本介绍:
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
类A通过接口interface1依赖(使用)类B,类C通过接口interface1依赖(使用)类D。如果接口interface1对于类A和类C来说不是最小接口,那么类B和类D必须实现他们不需要的方法
按照接口隔离原则应该这样处理:
将接口interface1拆分成独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系,也就是采用接口隔离原则
案例:
类A通过接口interface1依赖类B A中只会使用到接口的1,2,3方法
类C通过接口interface1依赖类D C中只会使用到接口的1,4,5方法
方式一:
1 package cn.rabcheng.interfacesegregation; 2 3 /** 4 * @auther cheng 5 * @create 2020-08-12 23:11 6 * 接口隔离原则 7 */ 8 public class InterfaceSegregation1 { 9 public static void main(String[] args) { 10 11 A a = new A(); 12 B b = new B(); 13 14 a.depend1(b); 15 a.depend2(b); 16 a.depend3(b); 17 18 C c = new C(); 19 D d = new D(); 20 21 c.depend1(d); 22 c.depend4(d); 23 c.depend5(d); 24 } 25 } 26 27 interface Interface1 { 28 void operation1(); 29 30 void operation2(); 31 32 void operation3(); 33 34 void operation4(); 35 36 void operation5(); 37 } 38 39 class B implements Interface1{ 40 41 @Override 42 public void operation1() { 43 System.out.println("B 实现了 operation1"); 44 } 45 46 @Override 47 public void operation2() { 48 System.out.println("B 实现了 operation2"); 49 } 50 51 @Override 52 public void operation3() { 53 System.out.println("B 实现了 operation3"); 54 } 55 56 @Override 57 public void operation4() { 58 System.out.println("B 实现了 operation4"); 59 } 60 61 @Override 62 public void operation5() { 63 System.out.println("B 实现了 operation5"); 64 } 65 } 66 67 class D implements Interface1{ 68 @Override 69 public void operation1() { 70 System.out.println("D 实现了 operation1"); 71 } 72 73 @Override 74 public void operation2() { 75 System.out.println("D 实现了 operation2"); 76 } 77 78 @Override 79 public void operation3() { 80 System.out.println("D 实现了 operation3"); 81 } 82 83 @Override 84 public void operation4() { 85 System.out.println("D 实现了 operation4"); 86 } 87 88 @Override 89 public void operation5() { 90 System.out.println("D 实现了 operation5"); 91 } 92 } 93 94 95 class A{ 96 public void depend1(Interface1 interface1){ 97 interface1.operation1(); 98 } 99 100 public void depend2(Interface1 interface1){ 101 interface1.operation2(); 102 } 103 104 public void depend3(Interface1 interface1){ 105 interface1.operation3(); 106 } 107 } 108 109 class C{ 110 public void depend1(Interface1 interface1){ 111 interface1.operation1(); 112 113 } 114 public void depend4(Interface1 interface1){ 115 interface1.operation4(); 116 117 } 118 public void depend5(Interface1 interface1){ 119 interface1.operation5(); 120 121 } 122 } 123 B 实现了 operation1 124 B 实现了 operation2 125 B 实现了 operation3 126 D 实现了 operation1 127 D 实现了 operation4 128 D 实现了 operation5
存在的问题:
接口interface1对于类A和类C来说不是最小接口,但是类B和类D必须实现他们不需要的方法
改进:
拆分接口interface1,则B、D不用实现他们不需要的方法
方式二:
1 package cn.rabcheng.interfacesegregation; 2 3 /** 4 * @auther cheng 5 * @create 2020-08-12 23:11 6 * 接口隔离原则 7 */ 8 public class InterfaceSegregation2 { 9 public static void main(String[] args) { 10 A2 a2 = new A2(); 11 B2 b2 = new B2(); 12 a2.depend1(b2); 13 a2.depend2(b2); 14 a2.depend3(b2); 15 16 C2 c2 = new C2(); 17 D2 d2 = new D2(); 18 c2.depend1(d2); 19 c2.depend4(d2); 20 c2.depend5(d2); 21 } 22 } 23 24 interface Interface2 { 25 void operation1(); 26 27 } 28 29 interface Interface3 { 30 void operation2(); 31 32 void operation3(); 33 } 34 35 interface Interface4 { 36 void operation4(); 37 38 void operation5(); 39 } 40 41 class B2 implements Interface2, Interface3 { 42 @Override 43 public void operation1() { 44 System.out.println("B2 实现了operation1"); 45 } 46 47 @Override 48 public void operation2() { 49 System.out.println("B2 实现了operation2"); 50 } 51 52 @Override 53 public void operation3() { 54 System.out.println("B2 实现了operation3"); 55 } 56 } 57 58 class D2 implements Interface2, Interface4 { 59 @Override 60 public void operation1() { 61 System.out.println("D2 实现了operation1"); 62 } 63 64 @Override 65 public void operation4() { 66 System.out.println("D2 实现了operation4"); 67 } 68 69 @Override 70 public void operation5() { 71 System.out.println("D2 实现了operation5"); 72 } 73 } 74 75 class A2 { 76 public void depend1(Interface2 interface2) { 77 interface2.operation1(); 78 } 79 80 public void depend2(Interface3 interface3) { 81 interface3.operation2(); 82 } 83 84 public void depend3(Interface3 interface3) { 85 interface3.operation3(); 86 } 87 } 88 89 class C2{ 90 public void depend1(Interface2 interface2){ 91 interface2.operation1(); 92 } 93 public void depend4(Interface4 interface4){ 94 interface4.operation4(); 95 } 96 public void depend5(Interface4 interface4){ 97 interface4.operation5(); 98 } 99 } 100 B2 实现了operation1 101 B2 实现了operation2 102 B2 实现了operation3 103 D2 实现了operation1 104 D2 实现了operation4 105 D2 实现了operation5
类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法
将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
接口Interface1中出现的方法,根据实际情况拆分为三个接口